
<!DOCTYPE html>

<html>
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>pymatgen.core.lattice &#8212; pymatgen 2020.7.3 documentation</title>
    <link rel="stylesheet" href="../../../_static/basic.css" type="text/css" />
    <link rel="stylesheet" href="../../../_static/pygments.css" type="text/css" />
    <script id="documentation_options" data-url_root="../../../" src="../../../_static/documentation_options.js"></script>
    <script src="../../../_static/jquery.js"></script>
    <script src="../../../_static/underscore.js"></script>
    <script src="../../../_static/doctools.js"></script>
    <script src="../../../_static/language_data.js"></script>
    <script async="async" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.7/latest.js?config=TeX-AMS-MML_HTMLorMML"></script>
    <link rel="index" title="Index" href="../../../genindex.html" />
    <link rel="search" title="Search" href="../../../search.html" />
 
<script type="text/javascript">
  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-33990148-1']);
  _gaq.push(['_trackPageview']);
</script>

  </head><body>
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="nav-item nav-item-0"><a href="../../../index.html">pymatgen 2020.7.3 documentation</a> &#187;</li>
          <li class="nav-item nav-item-1"><a href="../../index.html" >Module code</a> &#187;</li>
          <li class="nav-item nav-item-2"><a href="../../pymatgen.html" accesskey="U">pymatgen</a> &#187;</li>
        <li class="nav-item nav-item-this"><a href="">pymatgen.core.lattice</a></li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
          <div class="body" role="main">
            
  <h1>Source code for pymatgen.core.lattice</h1><div class="highlight"><pre>
<span></span><span class="c1"># coding: utf-8</span>
<span class="c1"># Copyright (c) Pymatgen Development Team.</span>
<span class="c1"># Distributed under the terms of the MIT License.</span>

<span class="sd">&quot;&quot;&quot;</span>
<span class="sd">Defines the classes relating to 3D lattices.</span>
<span class="sd">&quot;&quot;&quot;</span>

<span class="kn">import</span> <span class="nn">math</span>
<span class="kn">import</span> <span class="nn">itertools</span>
<span class="kn">import</span> <span class="nn">warnings</span>
<span class="kn">from</span> <span class="nn">functools</span> <span class="kn">import</span> <span class="n">reduce</span>
<span class="kn">import</span> <span class="nn">collections</span>

<span class="kn">from</span> <span class="nn">fractions</span> <span class="kn">import</span> <span class="n">Fraction</span>
<span class="kn">from</span> <span class="nn">typing</span> <span class="kn">import</span> <span class="n">List</span><span class="p">,</span> <span class="n">Union</span><span class="p">,</span> <span class="n">Dict</span><span class="p">,</span> <span class="n">Tuple</span><span class="p">,</span> <span class="n">Iterator</span><span class="p">,</span> <span class="n">Optional</span><span class="p">,</span> <span class="n">Sequence</span>

<span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<span class="kn">from</span> <span class="nn">numpy.linalg</span> <span class="kn">import</span> <span class="n">inv</span>
<span class="kn">from</span> <span class="nn">numpy</span> <span class="kn">import</span> <span class="n">pi</span><span class="p">,</span> <span class="n">dot</span><span class="p">,</span> <span class="n">transpose</span>

<span class="kn">from</span> <span class="nn">monty.json</span> <span class="kn">import</span> <span class="n">MSONable</span>
<span class="kn">from</span> <span class="nn">monty.dev</span> <span class="kn">import</span> <span class="n">deprecated</span>

<span class="kn">from</span> <span class="nn">pymatgen.util.coord</span> <span class="kn">import</span> <span class="n">pbc_shortest_vectors</span>
<span class="kn">from</span> <span class="nn">pymatgen.util.num</span> <span class="kn">import</span> <span class="n">abs_cap</span>
<span class="kn">from</span> <span class="nn">pymatgen.util.typing</span> <span class="kn">import</span> <span class="n">Vector3Like</span>


<span class="n">__author__</span> <span class="o">=</span> <span class="s2">&quot;Shyue Ping Ong, Michael Kocher&quot;</span>
<span class="n">__copyright__</span> <span class="o">=</span> <span class="s2">&quot;Copyright 2011, The Materials Project&quot;</span>
<span class="n">__maintainer__</span> <span class="o">=</span> <span class="s2">&quot;Shyue Ping Ong&quot;</span>
<span class="n">__email__</span> <span class="o">=</span> <span class="s2">&quot;shyuep@gmail.com&quot;</span>


<div class="viewcode-block" id="Lattice"><a class="viewcode-back" href="../../../pymatgen.core.lattice.html#pymatgen.core.lattice.Lattice">[docs]</a><span class="k">class</span> <span class="nc">Lattice</span><span class="p">(</span><span class="n">MSONable</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    A lattice object.  Essentially a matrix with conversion matrices. In</span>
<span class="sd">    general, it is assumed that length units are in Angstroms and angles are in</span>
<span class="sd">    degrees unless otherwise stated.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="c1"># Properties lazily generated for efficiency.</span>

    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">matrix</span><span class="p">:</span> <span class="n">Union</span><span class="p">[</span><span class="n">List</span><span class="p">[</span><span class="nb">float</span><span class="p">],</span> <span class="n">List</span><span class="p">[</span><span class="n">List</span><span class="p">[</span><span class="nb">float</span><span class="p">]],</span> <span class="n">np</span><span class="o">.</span><span class="n">ndarray</span><span class="p">]):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Create a lattice from any sequence of 9 numbers. Note that the sequence</span>
<span class="sd">        is assumed to be read one row at a time. Each row represents one</span>
<span class="sd">        lattice vector.</span>

<span class="sd">        Args:</span>
<span class="sd">            matrix: Sequence of numbers in any form. Examples of acceptable</span>
<span class="sd">                input.</span>
<span class="sd">                i) An actual numpy array.</span>
<span class="sd">                ii) [[1, 0, 0], [0, 1, 0], [0, 0, 1]]</span>
<span class="sd">                iii) [1, 0, 0 , 0, 1, 0, 0, 0, 1]</span>
<span class="sd">                iv) (1, 0, 0, 0, 1, 0, 0, 0, 1)</span>
<span class="sd">                Each row should correspond to a lattice vector.</span>
<span class="sd">                E.g., [[10, 0, 0], [20, 10, 0], [0, 0, 30]] specifies a lattice</span>
<span class="sd">                with lattice vectors [10, 0, 0], [20, 10, 0] and [0, 0, 30].</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">m</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">matrix</span><span class="p">,</span> <span class="n">dtype</span><span class="o">=</span><span class="n">np</span><span class="o">.</span><span class="n">float64</span><span class="p">)</span><span class="o">.</span><span class="n">reshape</span><span class="p">((</span><span class="mi">3</span><span class="p">,</span> <span class="mi">3</span><span class="p">))</span>
        <span class="n">m</span><span class="o">.</span><span class="n">setflags</span><span class="p">(</span><span class="n">write</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_matrix</span> <span class="o">=</span> <span class="n">m</span>  <span class="c1"># type: np.ndarray</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_inv_matrix</span> <span class="o">=</span> <span class="kc">None</span>  <span class="c1"># type: Optional[np.ndarray]</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_diags</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_lll_matrix_mappings</span> <span class="o">=</span> <span class="p">{}</span>  <span class="c1"># type: Dict[float, np.ndarray]</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_lll_inverse</span> <span class="o">=</span> <span class="kc">None</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">lengths</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">Tuple</span><span class="p">[</span><span class="nb">float</span><span class="p">,</span> <span class="nb">float</span><span class="p">,</span> <span class="nb">float</span><span class="p">]:</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        :return: The lengths (a, b, c) of the lattice.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="nb">tuple</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">sqrt</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">sum</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_matrix</span> <span class="o">**</span> <span class="mi">2</span><span class="p">,</span> <span class="n">axis</span><span class="o">=</span><span class="mi">1</span><span class="p">))</span><span class="o">.</span><span class="n">tolist</span><span class="p">())</span>  <span class="c1"># type: ignore</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">angles</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">Tuple</span><span class="p">[</span><span class="nb">float</span><span class="p">,</span> <span class="nb">float</span><span class="p">,</span> <span class="nb">float</span><span class="p">]:</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Returns the angles (alpha, beta, gamma) of the lattice.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">m</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_matrix</span>
        <span class="n">lengths</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">lengths</span>
        <span class="n">angles</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">(</span><span class="mi">3</span><span class="p">)</span>
        <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">3</span><span class="p">):</span>
            <span class="n">j</span> <span class="o">=</span> <span class="p">(</span><span class="n">i</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span> <span class="o">%</span> <span class="mi">3</span>
            <span class="n">k</span> <span class="o">=</span> <span class="p">(</span><span class="n">i</span> <span class="o">+</span> <span class="mi">2</span><span class="p">)</span> <span class="o">%</span> <span class="mi">3</span>
            <span class="n">angles</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="n">abs_cap</span><span class="p">(</span><span class="n">dot</span><span class="p">(</span><span class="n">m</span><span class="p">[</span><span class="n">j</span><span class="p">],</span> <span class="n">m</span><span class="p">[</span><span class="n">k</span><span class="p">])</span> <span class="o">/</span> <span class="p">(</span><span class="n">lengths</span><span class="p">[</span><span class="n">j</span><span class="p">]</span> <span class="o">*</span> <span class="n">lengths</span><span class="p">[</span><span class="n">k</span><span class="p">]))</span>
        <span class="n">angles</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">arccos</span><span class="p">(</span><span class="n">angles</span><span class="p">)</span> <span class="o">*</span> <span class="mf">180.0</span> <span class="o">/</span> <span class="n">pi</span>
        <span class="k">return</span> <span class="nb">tuple</span><span class="p">(</span><span class="n">angles</span><span class="o">.</span><span class="n">tolist</span><span class="p">())</span>  <span class="c1"># type: ignore</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">is_orthogonal</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="nb">bool</span><span class="p">:</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        :return: Whether all angles are 90 degrees.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="nb">all</span><span class="p">([</span><span class="nb">abs</span><span class="p">(</span><span class="n">a</span> <span class="o">-</span> <span class="mi">90</span><span class="p">)</span> <span class="o">&lt;</span> <span class="mf">1e-5</span> <span class="k">for</span> <span class="n">a</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">angles</span><span class="p">])</span>

    <span class="k">def</span> <span class="fm">__format__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">fmt_spec</span><span class="o">=</span><span class="s2">&quot;&quot;</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Support format printing. Supported formats are:</span>

<span class="sd">        1. &quot;l&quot; for a list format that can be easily copied and pasted, e.g.,</span>
<span class="sd">           &quot;.3fl&quot; prints something like</span>
<span class="sd">           &quot;[[10.000, 0.000, 0.000], [0.000, 10.000, 0.000], [0.000, 0.000, 10.000]]&quot;</span>
<span class="sd">        2. &quot;p&quot; for lattice parameters &quot;.1fp&quot; prints something like</span>
<span class="sd">           &quot;{10.0, 10.0, 10.0, 90.0, 90.0, 90.0}&quot;</span>
<span class="sd">        3. Default will simply print a 3x3 matrix form. E.g.,</span>
<span class="sd">           10.000 0.000 0.000</span>
<span class="sd">           0.000 10.000 0.000</span>
<span class="sd">           0.000 0.000 10.000</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">m</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_matrix</span><span class="o">.</span><span class="n">tolist</span><span class="p">()</span>
        <span class="k">if</span> <span class="n">fmt_spec</span><span class="o">.</span><span class="n">endswith</span><span class="p">(</span><span class="s2">&quot;l&quot;</span><span class="p">):</span>
            <span class="n">fmt</span> <span class="o">=</span> <span class="s2">&quot;[[</span><span class="si">{}</span><span class="s2">, </span><span class="si">{}</span><span class="s2">, </span><span class="si">{}</span><span class="s2">], [</span><span class="si">{}</span><span class="s2">, </span><span class="si">{}</span><span class="s2">, </span><span class="si">{}</span><span class="s2">], [</span><span class="si">{}</span><span class="s2">, </span><span class="si">{}</span><span class="s2">, </span><span class="si">{}</span><span class="s2">]]&quot;</span>
            <span class="n">fmt_spec</span> <span class="o">=</span> <span class="n">fmt_spec</span><span class="p">[:</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>
        <span class="k">elif</span> <span class="n">fmt_spec</span><span class="o">.</span><span class="n">endswith</span><span class="p">(</span><span class="s2">&quot;p&quot;</span><span class="p">):</span>
            <span class="n">fmt</span> <span class="o">=</span> <span class="s2">&quot;{{</span><span class="si">{}</span><span class="s2">, </span><span class="si">{}</span><span class="s2">, </span><span class="si">{}</span><span class="s2">, </span><span class="si">{}</span><span class="s2">, </span><span class="si">{}</span><span class="s2">, </span><span class="si">{}</span><span class="s2">}}&quot;</span>
            <span class="n">fmt_spec</span> <span class="o">=</span> <span class="n">fmt_spec</span><span class="p">[:</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>
            <span class="n">m</span> <span class="o">=</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">lengths</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">angles</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">fmt</span> <span class="o">=</span> <span class="s2">&quot;</span><span class="si">{}</span><span class="s2"> </span><span class="si">{}</span><span class="s2"> </span><span class="si">{}</span><span class="se">\n</span><span class="si">{}</span><span class="s2"> </span><span class="si">{}</span><span class="s2"> </span><span class="si">{}</span><span class="se">\n</span><span class="si">{}</span><span class="s2"> </span><span class="si">{}</span><span class="s2"> </span><span class="si">{}</span><span class="s2">&quot;</span>
        <span class="k">return</span> <span class="n">fmt</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="o">*</span><span class="p">[</span><span class="nb">format</span><span class="p">(</span><span class="n">c</span><span class="p">,</span> <span class="n">fmt_spec</span><span class="p">)</span> <span class="k">for</span> <span class="n">row</span> <span class="ow">in</span> <span class="n">m</span> <span class="k">for</span> <span class="n">c</span> <span class="ow">in</span> <span class="n">row</span><span class="p">])</span>

<div class="viewcode-block" id="Lattice.copy"><a class="viewcode-back" href="../../../pymatgen.core.lattice.html#pymatgen.core.lattice.Lattice.copy">[docs]</a>    <span class="k">def</span> <span class="nf">copy</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Deep copy of self.&quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="vm">__class__</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">matrix</span><span class="o">.</span><span class="n">copy</span><span class="p">())</span></div>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">matrix</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">np</span><span class="o">.</span><span class="n">ndarray</span><span class="p">:</span>
        <span class="sd">&quot;&quot;&quot;Copy of matrix representing the Lattice&quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_matrix</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">inv_matrix</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">np</span><span class="o">.</span><span class="n">ndarray</span><span class="p">:</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Inverse of lattice matrix.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_inv_matrix</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_inv_matrix</span> <span class="o">=</span> <span class="n">inv</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_matrix</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_inv_matrix</span><span class="o">.</span><span class="n">setflags</span><span class="p">(</span><span class="n">write</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_inv_matrix</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">metric_tensor</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">np</span><span class="o">.</span><span class="n">ndarray</span><span class="p">:</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        The metric tensor of the lattice.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="n">dot</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_matrix</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">_matrix</span><span class="o">.</span><span class="n">T</span><span class="p">)</span>

<div class="viewcode-block" id="Lattice.get_cartesian_coords"><a class="viewcode-back" href="../../../pymatgen.core.lattice.html#pymatgen.core.lattice.Lattice.get_cartesian_coords">[docs]</a>    <span class="k">def</span> <span class="nf">get_cartesian_coords</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">fractional_coords</span><span class="p">:</span> <span class="n">Vector3Like</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">np</span><span class="o">.</span><span class="n">ndarray</span><span class="p">:</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Returns the cartesian coordinates given fractional coordinates.</span>

<span class="sd">        Args:</span>
<span class="sd">            fractional_coords (3x1 array): Fractional coords.</span>

<span class="sd">        Returns:</span>
<span class="sd">            Cartesian coordinates</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="n">dot</span><span class="p">(</span><span class="n">fractional_coords</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">_matrix</span><span class="p">)</span></div>

<div class="viewcode-block" id="Lattice.get_fractional_coords"><a class="viewcode-back" href="../../../pymatgen.core.lattice.html#pymatgen.core.lattice.Lattice.get_fractional_coords">[docs]</a>    <span class="k">def</span> <span class="nf">get_fractional_coords</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">cart_coords</span><span class="p">:</span> <span class="n">Vector3Like</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">np</span><span class="o">.</span><span class="n">ndarray</span><span class="p">:</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Returns the fractional coordinates given cartesian coordinates.</span>

<span class="sd">        Args:</span>
<span class="sd">            cart_coords (3x1 array): Cartesian coords.</span>

<span class="sd">        Returns:</span>
<span class="sd">            Fractional coordinates.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="n">dot</span><span class="p">(</span><span class="n">cart_coords</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">inv_matrix</span><span class="p">)</span></div>

<div class="viewcode-block" id="Lattice.get_vector_along_lattice_directions"><a class="viewcode-back" href="../../../pymatgen.core.lattice.html#pymatgen.core.lattice.Lattice.get_vector_along_lattice_directions">[docs]</a>    <span class="k">def</span> <span class="nf">get_vector_along_lattice_directions</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">cart_coords</span><span class="p">:</span> <span class="n">Vector3Like</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Returns the coordinates along lattice directions given cartesian coordinates.</span>

<span class="sd">        Note, this is different than a projection of the cartesian vector along the</span>
<span class="sd">        lattice parameters. It is simply the fractional coordinates multiplied by the</span>
<span class="sd">        lattice vector magnitudes.</span>

<span class="sd">        For example, this method is helpful when analyzing the dipole moment (in</span>
<span class="sd">        units of electron Angstroms) of a ferroelectric crystal. See the `Polarization`</span>
<span class="sd">        class in `pymatgen.analysis.ferroelectricity.polarization`.</span>

<span class="sd">        Args:</span>
<span class="sd">            cart_coords (3x1 array): Cartesian coords.</span>

<span class="sd">        Returns:</span>
<span class="sd">            Lattice coordinates.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">lengths</span> <span class="o">*</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_fractional_coords</span><span class="p">(</span><span class="n">cart_coords</span><span class="p">)</span></div>

<div class="viewcode-block" id="Lattice.d_hkl"><a class="viewcode-back" href="../../../pymatgen.core.lattice.html#pymatgen.core.lattice.Lattice.d_hkl">[docs]</a>    <span class="k">def</span> <span class="nf">d_hkl</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">miller_index</span><span class="p">:</span> <span class="n">Vector3Like</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="nb">float</span><span class="p">:</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Returns the distance between the hkl plane and the origin</span>

<span class="sd">        Args:</span>
<span class="sd">            miller_index ([h,k,l]): Miller index of plane</span>

<span class="sd">        Returns:</span>
<span class="sd">            d_hkl (float)</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="n">gstar</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">reciprocal_lattice_crystallographic</span><span class="o">.</span><span class="n">metric_tensor</span>
        <span class="n">hkl</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">miller_index</span><span class="p">)</span>
        <span class="k">return</span> <span class="mi">1</span> <span class="o">/</span> <span class="p">((</span><span class="n">dot</span><span class="p">(</span><span class="n">dot</span><span class="p">(</span><span class="n">hkl</span><span class="p">,</span> <span class="n">gstar</span><span class="p">),</span> <span class="n">hkl</span><span class="o">.</span><span class="n">T</span><span class="p">))</span> <span class="o">**</span> <span class="p">(</span><span class="mi">1</span> <span class="o">/</span> <span class="mi">2</span><span class="p">))</span></div>

<div class="viewcode-block" id="Lattice.cubic"><a class="viewcode-back" href="../../../pymatgen.core.lattice.html#pymatgen.core.lattice.Lattice.cubic">[docs]</a>    <span class="nd">@staticmethod</span>
    <span class="k">def</span> <span class="nf">cubic</span><span class="p">(</span><span class="n">a</span><span class="p">:</span> <span class="nb">float</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Convenience constructor for a cubic lattice.</span>

<span class="sd">        Args:</span>
<span class="sd">            a (float): The *a* lattice parameter of the cubic cell.</span>

<span class="sd">        Returns:</span>
<span class="sd">            Cubic lattice of dimensions a x a x a.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="n">Lattice</span><span class="p">([[</span><span class="n">a</span><span class="p">,</span> <span class="mf">0.0</span><span class="p">,</span> <span class="mf">0.0</span><span class="p">],</span> <span class="p">[</span><span class="mf">0.0</span><span class="p">,</span> <span class="n">a</span><span class="p">,</span> <span class="mf">0.0</span><span class="p">],</span> <span class="p">[</span><span class="mf">0.0</span><span class="p">,</span> <span class="mf">0.0</span><span class="p">,</span> <span class="n">a</span><span class="p">]])</span></div>

<div class="viewcode-block" id="Lattice.tetragonal"><a class="viewcode-back" href="../../../pymatgen.core.lattice.html#pymatgen.core.lattice.Lattice.tetragonal">[docs]</a>    <span class="nd">@staticmethod</span>
    <span class="k">def</span> <span class="nf">tetragonal</span><span class="p">(</span><span class="n">a</span><span class="p">:</span> <span class="nb">float</span><span class="p">,</span> <span class="n">c</span><span class="p">:</span> <span class="nb">float</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Convenience constructor for a tetragonal lattice.</span>

<span class="sd">        Args:</span>
<span class="sd">            a (float): *a* lattice parameter of the tetragonal cell.</span>
<span class="sd">            c (float): *c* lattice parameter of the tetragonal cell.</span>

<span class="sd">        Returns:</span>
<span class="sd">            Tetragonal lattice of dimensions a x a x c.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="n">Lattice</span><span class="o">.</span><span class="n">from_parameters</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">a</span><span class="p">,</span> <span class="n">c</span><span class="p">,</span> <span class="mi">90</span><span class="p">,</span> <span class="mi">90</span><span class="p">,</span> <span class="mi">90</span><span class="p">)</span></div>

<div class="viewcode-block" id="Lattice.orthorhombic"><a class="viewcode-back" href="../../../pymatgen.core.lattice.html#pymatgen.core.lattice.Lattice.orthorhombic">[docs]</a>    <span class="nd">@staticmethod</span>
    <span class="k">def</span> <span class="nf">orthorhombic</span><span class="p">(</span><span class="n">a</span><span class="p">:</span> <span class="nb">float</span><span class="p">,</span> <span class="n">b</span><span class="p">:</span> <span class="nb">float</span><span class="p">,</span> <span class="n">c</span><span class="p">:</span> <span class="nb">float</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Convenience constructor for an orthorhombic lattice.</span>

<span class="sd">        Args:</span>
<span class="sd">            a (float): *a* lattice parameter of the orthorhombic cell.</span>
<span class="sd">            b (float): *b* lattice parameter of the orthorhombic cell.</span>
<span class="sd">            c (float): *c* lattice parameter of the orthorhombic cell.</span>

<span class="sd">        Returns:</span>
<span class="sd">            Orthorhombic lattice of dimensions a x b x c.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="n">Lattice</span><span class="o">.</span><span class="n">from_parameters</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">,</span> <span class="n">c</span><span class="p">,</span> <span class="mi">90</span><span class="p">,</span> <span class="mi">90</span><span class="p">,</span> <span class="mi">90</span><span class="p">)</span></div>

<div class="viewcode-block" id="Lattice.monoclinic"><a class="viewcode-back" href="../../../pymatgen.core.lattice.html#pymatgen.core.lattice.Lattice.monoclinic">[docs]</a>    <span class="nd">@staticmethod</span>
    <span class="k">def</span> <span class="nf">monoclinic</span><span class="p">(</span><span class="n">a</span><span class="p">:</span> <span class="nb">float</span><span class="p">,</span> <span class="n">b</span><span class="p">:</span> <span class="nb">float</span><span class="p">,</span> <span class="n">c</span><span class="p">:</span> <span class="nb">float</span><span class="p">,</span> <span class="n">beta</span><span class="p">:</span> <span class="nb">float</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Convenience constructor for a monoclinic lattice.</span>

<span class="sd">        Args:</span>
<span class="sd">            a (float): *a* lattice parameter of the monoclinc cell.</span>
<span class="sd">            b (float): *b* lattice parameter of the monoclinc cell.</span>
<span class="sd">            c (float): *c* lattice parameter of the monoclinc cell.</span>
<span class="sd">            beta (float): *beta* angle between lattice vectors b and c in</span>
<span class="sd">                degrees.</span>

<span class="sd">        Returns:</span>
<span class="sd">            Monoclinic lattice of dimensions a x b x c with non right-angle</span>
<span class="sd">            beta between lattice vectors a and c.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="n">Lattice</span><span class="o">.</span><span class="n">from_parameters</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">,</span> <span class="n">c</span><span class="p">,</span> <span class="mi">90</span><span class="p">,</span> <span class="n">beta</span><span class="p">,</span> <span class="mi">90</span><span class="p">)</span></div>

<div class="viewcode-block" id="Lattice.hexagonal"><a class="viewcode-back" href="../../../pymatgen.core.lattice.html#pymatgen.core.lattice.Lattice.hexagonal">[docs]</a>    <span class="nd">@staticmethod</span>
    <span class="k">def</span> <span class="nf">hexagonal</span><span class="p">(</span><span class="n">a</span><span class="p">:</span> <span class="nb">float</span><span class="p">,</span> <span class="n">c</span><span class="p">:</span> <span class="nb">float</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Convenience constructor for a hexagonal lattice.</span>

<span class="sd">        Args:</span>
<span class="sd">            a (float): *a* lattice parameter of the hexagonal cell.</span>
<span class="sd">            c (float): *c* lattice parameter of the hexagonal cell.</span>

<span class="sd">        Returns:</span>
<span class="sd">            Hexagonal lattice of dimensions a x a x c.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="n">Lattice</span><span class="o">.</span><span class="n">from_parameters</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">a</span><span class="p">,</span> <span class="n">c</span><span class="p">,</span> <span class="mi">90</span><span class="p">,</span> <span class="mi">90</span><span class="p">,</span> <span class="mi">120</span><span class="p">)</span></div>

<div class="viewcode-block" id="Lattice.rhombohedral"><a class="viewcode-back" href="../../../pymatgen.core.lattice.html#pymatgen.core.lattice.Lattice.rhombohedral">[docs]</a>    <span class="nd">@staticmethod</span>
    <span class="k">def</span> <span class="nf">rhombohedral</span><span class="p">(</span><span class="n">a</span><span class="p">:</span> <span class="nb">float</span><span class="p">,</span> <span class="n">alpha</span><span class="p">:</span> <span class="nb">float</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Convenience constructor for a rhombohedral lattice.</span>

<span class="sd">        Args:</span>
<span class="sd">            a (float): *a* lattice parameter of the rhombohedral cell.</span>
<span class="sd">            alpha (float): Angle for the rhombohedral lattice in degrees.</span>

<span class="sd">        Returns:</span>
<span class="sd">            Rhombohedral lattice of dimensions a x a x a.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="n">Lattice</span><span class="o">.</span><span class="n">from_parameters</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">a</span><span class="p">,</span> <span class="n">a</span><span class="p">,</span> <span class="n">alpha</span><span class="p">,</span> <span class="n">alpha</span><span class="p">,</span> <span class="n">alpha</span><span class="p">)</span></div>

    <span class="nd">@staticmethod</span>
    <span class="nd">@deprecated</span><span class="p">(</span><span class="n">message</span><span class="o">=</span><span class="s2">&quot;Use Lattice.from_parameters instead. This will be removed in v2020.*&quot;</span><span class="p">)</span>
    <span class="k">def</span> <span class="nf">from_lengths_and_angles</span><span class="p">(</span><span class="n">abc</span><span class="p">:</span> <span class="n">Sequence</span><span class="p">[</span><span class="nb">float</span><span class="p">],</span> <span class="n">ang</span><span class="p">:</span> <span class="n">Sequence</span><span class="p">[</span><span class="nb">float</span><span class="p">]):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Create a Lattice using unit cell lengths and angles (in degrees).</span>

<span class="sd">        Args:</span>
<span class="sd">            abc (3x1 array): Lattice parameters, e.g. (4, 4, 5).</span>
<span class="sd">            ang (3x1 array): Lattice angles in degrees, e.g., (90,90,120).</span>

<span class="sd">        Returns:</span>
<span class="sd">            A Lattice with the specified lattice parameters.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="n">Lattice</span><span class="o">.</span><span class="n">from_parameters</span><span class="p">(</span><span class="n">abc</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">abc</span><span class="p">[</span><span class="mi">1</span><span class="p">],</span> <span class="n">abc</span><span class="p">[</span><span class="mi">2</span><span class="p">],</span> <span class="n">ang</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">ang</span><span class="p">[</span><span class="mi">1</span><span class="p">],</span> <span class="n">ang</span><span class="p">[</span><span class="mi">2</span><span class="p">])</span>

<div class="viewcode-block" id="Lattice.from_parameters"><a class="viewcode-back" href="../../../pymatgen.core.lattice.html#pymatgen.core.lattice.Lattice.from_parameters">[docs]</a>    <span class="nd">@classmethod</span>
    <span class="k">def</span> <span class="nf">from_parameters</span><span class="p">(</span>
            <span class="bp">cls</span><span class="p">,</span>
            <span class="n">a</span><span class="p">:</span> <span class="nb">float</span><span class="p">,</span>
            <span class="n">b</span><span class="p">:</span> <span class="nb">float</span><span class="p">,</span>
            <span class="n">c</span><span class="p">:</span> <span class="nb">float</span><span class="p">,</span>
            <span class="n">alpha</span><span class="p">:</span> <span class="nb">float</span><span class="p">,</span>
            <span class="n">beta</span><span class="p">:</span> <span class="nb">float</span><span class="p">,</span>
            <span class="n">gamma</span><span class="p">:</span> <span class="nb">float</span><span class="p">,</span>
            <span class="n">vesta</span><span class="p">:</span> <span class="nb">bool</span> <span class="o">=</span> <span class="kc">False</span><span class="p">,</span>
    <span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Create a Lattice using unit cell lengths and angles (in degrees).</span>

<span class="sd">        Args:</span>
<span class="sd">            a (float): *a* lattice parameter.</span>
<span class="sd">            b (float): *b* lattice parameter.</span>
<span class="sd">            c (float): *c* lattice parameter.</span>
<span class="sd">            alpha (float): *alpha* angle in degrees.</span>
<span class="sd">            beta (float): *beta* angle in degrees.</span>
<span class="sd">            gamma (float): *gamma* angle in degrees.</span>
<span class="sd">            vesta: True if you import Cartesian coordinates from VESTA.</span>

<span class="sd">        Returns:</span>
<span class="sd">            Lattice with the specified lattice parameters.</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="n">angles_r</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">radians</span><span class="p">([</span><span class="n">alpha</span><span class="p">,</span> <span class="n">beta</span><span class="p">,</span> <span class="n">gamma</span><span class="p">])</span>
        <span class="n">cos_alpha</span><span class="p">,</span> <span class="n">cos_beta</span><span class="p">,</span> <span class="n">cos_gamma</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">cos</span><span class="p">(</span><span class="n">angles_r</span><span class="p">)</span>
        <span class="n">sin_alpha</span><span class="p">,</span> <span class="n">sin_beta</span><span class="p">,</span> <span class="n">sin_gamma</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">sin</span><span class="p">(</span><span class="n">angles_r</span><span class="p">)</span>

        <span class="k">if</span> <span class="n">vesta</span><span class="p">:</span>
            <span class="n">c1</span> <span class="o">=</span> <span class="n">c</span> <span class="o">*</span> <span class="n">cos_beta</span>
            <span class="n">c2</span> <span class="o">=</span> <span class="p">(</span><span class="n">c</span> <span class="o">*</span> <span class="p">(</span><span class="n">cos_alpha</span> <span class="o">-</span> <span class="p">(</span><span class="n">cos_beta</span> <span class="o">*</span> <span class="n">cos_gamma</span><span class="p">)))</span> <span class="o">/</span> <span class="n">sin_gamma</span>

            <span class="n">vector_a</span> <span class="o">=</span> <span class="p">[</span><span class="nb">float</span><span class="p">(</span><span class="n">a</span><span class="p">),</span> <span class="mf">0.0</span><span class="p">,</span> <span class="mf">0.0</span><span class="p">]</span>
            <span class="n">vector_b</span> <span class="o">=</span> <span class="p">[</span><span class="n">b</span> <span class="o">*</span> <span class="n">cos_gamma</span><span class="p">,</span> <span class="n">b</span> <span class="o">*</span> <span class="n">sin_gamma</span><span class="p">,</span> <span class="mi">0</span><span class="p">]</span>
            <span class="n">vector_c</span> <span class="o">=</span> <span class="p">[</span><span class="n">c1</span><span class="p">,</span> <span class="n">c2</span><span class="p">,</span> <span class="n">math</span><span class="o">.</span><span class="n">sqrt</span><span class="p">(</span><span class="n">c</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">-</span> <span class="n">c1</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">-</span> <span class="n">c2</span> <span class="o">**</span> <span class="mi">2</span><span class="p">)]</span>

        <span class="k">else</span><span class="p">:</span>
            <span class="n">val</span> <span class="o">=</span> <span class="p">(</span><span class="n">cos_alpha</span> <span class="o">*</span> <span class="n">cos_beta</span> <span class="o">-</span> <span class="n">cos_gamma</span><span class="p">)</span> <span class="o">/</span> <span class="p">(</span><span class="n">sin_alpha</span> <span class="o">*</span> <span class="n">sin_beta</span><span class="p">)</span>
            <span class="c1"># Sometimes rounding errors result in values slightly &gt; 1.</span>
            <span class="n">val</span> <span class="o">=</span> <span class="n">abs_cap</span><span class="p">(</span><span class="n">val</span><span class="p">)</span>
            <span class="n">gamma_star</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">arccos</span><span class="p">(</span><span class="n">val</span><span class="p">)</span>

            <span class="n">vector_a</span> <span class="o">=</span> <span class="p">[</span><span class="n">a</span> <span class="o">*</span> <span class="n">sin_beta</span><span class="p">,</span> <span class="mf">0.0</span><span class="p">,</span> <span class="n">a</span> <span class="o">*</span> <span class="n">cos_beta</span><span class="p">]</span>
            <span class="n">vector_b</span> <span class="o">=</span> <span class="p">[</span>
                <span class="o">-</span><span class="n">b</span> <span class="o">*</span> <span class="n">sin_alpha</span> <span class="o">*</span> <span class="n">np</span><span class="o">.</span><span class="n">cos</span><span class="p">(</span><span class="n">gamma_star</span><span class="p">),</span>
                <span class="n">b</span> <span class="o">*</span> <span class="n">sin_alpha</span> <span class="o">*</span> <span class="n">np</span><span class="o">.</span><span class="n">sin</span><span class="p">(</span><span class="n">gamma_star</span><span class="p">),</span>
                <span class="n">b</span> <span class="o">*</span> <span class="n">cos_alpha</span><span class="p">,</span>
            <span class="p">]</span>
            <span class="n">vector_c</span> <span class="o">=</span> <span class="p">[</span><span class="mf">0.0</span><span class="p">,</span> <span class="mf">0.0</span><span class="p">,</span> <span class="nb">float</span><span class="p">(</span><span class="n">c</span><span class="p">)]</span>

        <span class="k">return</span> <span class="n">Lattice</span><span class="p">([</span><span class="n">vector_a</span><span class="p">,</span> <span class="n">vector_b</span><span class="p">,</span> <span class="n">vector_c</span><span class="p">])</span></div>

<div class="viewcode-block" id="Lattice.from_dict"><a class="viewcode-back" href="../../../pymatgen.core.lattice.html#pymatgen.core.lattice.Lattice.from_dict">[docs]</a>    <span class="nd">@classmethod</span>
    <span class="k">def</span> <span class="nf">from_dict</span><span class="p">(</span><span class="bp">cls</span><span class="p">,</span> <span class="n">d</span><span class="p">:</span> <span class="n">Dict</span><span class="p">,</span> <span class="n">fmt</span><span class="p">:</span> <span class="nb">str</span> <span class="o">=</span> <span class="kc">None</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Create a Lattice from a dictionary containing the a, b, c, alpha, beta,</span>
<span class="sd">        and gamma parameters if fmt is None.</span>

<span class="sd">        If fmt == &quot;abivars&quot;, the function build a `Lattice` object from a</span>
<span class="sd">        dictionary with the Abinit variables `acell` and `rprim` in Bohr.</span>
<span class="sd">        If acell is not given, the Abinit default is used i.e. [1,1,1] Bohr</span>

<span class="sd">        Example:</span>

<span class="sd">            Lattice.from_dict(fmt=&quot;abivars&quot;, acell=3*[10], rprim=np.eye(3))</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="n">fmt</span> <span class="o">==</span> <span class="s2">&quot;abivars&quot;</span><span class="p">:</span>
            <span class="kn">from</span> <span class="nn">pymatgen.io.abinit.abiobjects</span> <span class="kn">import</span> <span class="n">lattice_from_abivars</span>

            <span class="n">kwargs</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="n">d</span><span class="p">)</span>
            <span class="k">return</span> <span class="n">lattice_from_abivars</span><span class="p">(</span><span class="bp">cls</span><span class="o">=</span><span class="bp">cls</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>

        <span class="k">if</span> <span class="s2">&quot;matrix&quot;</span> <span class="ow">in</span> <span class="n">d</span><span class="p">:</span>
            <span class="k">return</span> <span class="bp">cls</span><span class="p">(</span><span class="n">d</span><span class="p">[</span><span class="s2">&quot;matrix&quot;</span><span class="p">])</span>
        <span class="k">return</span> <span class="bp">cls</span><span class="o">.</span><span class="n">from_parameters</span><span class="p">(</span><span class="n">d</span><span class="p">[</span><span class="s2">&quot;a&quot;</span><span class="p">],</span> <span class="n">d</span><span class="p">[</span><span class="s2">&quot;b&quot;</span><span class="p">],</span> <span class="n">d</span><span class="p">[</span><span class="s2">&quot;c&quot;</span><span class="p">],</span>
                                   <span class="n">d</span><span class="p">[</span><span class="s2">&quot;alpha&quot;</span><span class="p">],</span> <span class="n">d</span><span class="p">[</span><span class="s2">&quot;beta&quot;</span><span class="p">],</span> <span class="n">d</span><span class="p">[</span><span class="s2">&quot;gamma&quot;</span><span class="p">])</span></div>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">a</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="nb">float</span><span class="p">:</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        *a* lattice parameter.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">lengths</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">b</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="nb">float</span><span class="p">:</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        *b* lattice parameter.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">lengths</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">c</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="nb">float</span><span class="p">:</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        *c* lattice parameter.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">lengths</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">abc</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">Tuple</span><span class="p">[</span><span class="nb">float</span><span class="p">,</span> <span class="nb">float</span><span class="p">,</span> <span class="nb">float</span><span class="p">]:</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Lengths of the lattice vectors, i.e. (a, b, c)</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">lengths</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">alpha</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="nb">float</span><span class="p">:</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Angle alpha of lattice in degrees.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">angles</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">beta</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="nb">float</span><span class="p">:</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Angle beta of lattice in degrees.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">angles</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">gamma</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="nb">float</span><span class="p">:</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Angle gamma of lattice in degrees.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">angles</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">volume</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="nb">float</span><span class="p">:</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Volume of the unit cell.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">m</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_matrix</span>
        <span class="k">return</span> <span class="nb">float</span><span class="p">(</span><span class="nb">abs</span><span class="p">(</span><span class="n">dot</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">cross</span><span class="p">(</span><span class="n">m</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">m</span><span class="p">[</span><span class="mi">1</span><span class="p">]),</span> <span class="n">m</span><span class="p">[</span><span class="mi">2</span><span class="p">])))</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">parameters</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">Tuple</span><span class="p">[</span><span class="nb">float</span><span class="p">,</span> <span class="nb">float</span><span class="p">,</span> <span class="nb">float</span><span class="p">,</span> <span class="nb">float</span><span class="p">,</span> <span class="nb">float</span><span class="p">,</span> <span class="nb">float</span><span class="p">]:</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Returns: (a, b, c, alpha, beta, gamma).</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="p">(</span><span class="o">*</span><span class="bp">self</span><span class="o">.</span><span class="n">lengths</span><span class="p">,</span> <span class="o">*</span><span class="bp">self</span><span class="o">.</span><span class="n">angles</span><span class="p">)</span>

    <span class="nd">@property</span>  <span class="c1"># type: ignore</span>
    <span class="nd">@deprecated</span><span class="p">(</span><span class="n">message</span><span class="o">=</span><span class="s2">&quot;Use Lattice.parameters instead. This will be removed in v2020.*&quot;</span><span class="p">)</span>
    <span class="k">def</span> <span class="nf">lengths_and_angles</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">Tuple</span><span class="p">[</span><span class="n">Tuple</span><span class="p">[</span><span class="nb">float</span><span class="p">,</span> <span class="nb">float</span><span class="p">,</span> <span class="nb">float</span><span class="p">],</span> <span class="n">Tuple</span><span class="p">[</span><span class="nb">float</span><span class="p">,</span> <span class="nb">float</span><span class="p">,</span> <span class="nb">float</span><span class="p">]]:</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Returns (lattice lengths, lattice angles).</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">lengths</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">angles</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">reciprocal_lattice</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="s2">&quot;Lattice&quot;</span><span class="p">:</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Return the reciprocal lattice. Note that this is the standard</span>
<span class="sd">        reciprocal lattice used for solid state physics with a factor of 2 *</span>
<span class="sd">        pi. If you are looking for the crystallographic reciprocal lattice,</span>
<span class="sd">        use the reciprocal_lattice_crystallographic property.</span>
<span class="sd">        The property is lazily generated for efficiency.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">v</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">linalg</span><span class="o">.</span><span class="n">inv</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_matrix</span><span class="p">)</span><span class="o">.</span><span class="n">T</span>
        <span class="k">return</span> <span class="n">Lattice</span><span class="p">(</span><span class="n">v</span> <span class="o">*</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">np</span><span class="o">.</span><span class="n">pi</span><span class="p">)</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">reciprocal_lattice_crystallographic</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="s2">&quot;Lattice&quot;</span><span class="p">:</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Returns the *crystallographic* reciprocal lattice, i.e., no factor of</span>
<span class="sd">        2 * pi.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="n">Lattice</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">reciprocal_lattice</span><span class="o">.</span><span class="n">matrix</span> <span class="o">/</span> <span class="p">(</span><span class="mi">2</span> <span class="o">*</span> <span class="n">np</span><span class="o">.</span><span class="n">pi</span><span class="p">))</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">lll_matrix</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">np</span><span class="o">.</span><span class="n">ndarray</span><span class="p">:</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        :return: The matrix for LLL reduction</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="mf">0.75</span> <span class="ow">not</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_lll_matrix_mappings</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_lll_matrix_mappings</span><span class="p">[</span><span class="mf">0.75</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_calculate_lll</span><span class="p">()</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_lll_matrix_mappings</span><span class="p">[</span><span class="mf">0.75</span><span class="p">][</span><span class="mi">0</span><span class="p">]</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">lll_mapping</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">np</span><span class="o">.</span><span class="n">ndarray</span><span class="p">:</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        :return: The mapping between the LLL reduced lattice and the original</span>
<span class="sd">            lattice.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="mf">0.75</span> <span class="ow">not</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_lll_matrix_mappings</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_lll_matrix_mappings</span><span class="p">[</span><span class="mf">0.75</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_calculate_lll</span><span class="p">()</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_lll_matrix_mappings</span><span class="p">[</span><span class="mf">0.75</span><span class="p">][</span><span class="mi">1</span><span class="p">]</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">lll_inverse</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">np</span><span class="o">.</span><span class="n">ndarray</span><span class="p">:</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        :return: Inverse of self.lll_mapping.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="n">np</span><span class="o">.</span><span class="n">linalg</span><span class="o">.</span><span class="n">inv</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">lll_mapping</span><span class="p">)</span>

    <span class="k">def</span> <span class="fm">__repr__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="n">outs</span> <span class="o">=</span> <span class="p">[</span>
            <span class="s2">&quot;Lattice&quot;</span><span class="p">,</span>
            <span class="s2">&quot;    abc : &quot;</span> <span class="o">+</span> <span class="s2">&quot; &quot;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="nb">map</span><span class="p">(</span><span class="nb">repr</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">lengths</span><span class="p">)),</span>
            <span class="s2">&quot; angles : &quot;</span> <span class="o">+</span> <span class="s2">&quot; &quot;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="nb">map</span><span class="p">(</span><span class="nb">repr</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">angles</span><span class="p">)),</span>
            <span class="s2">&quot; volume : &quot;</span> <span class="o">+</span> <span class="nb">repr</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">volume</span><span class="p">),</span>
            <span class="s2">&quot;      A : &quot;</span> <span class="o">+</span> <span class="s2">&quot; &quot;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="nb">map</span><span class="p">(</span><span class="nb">repr</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">_matrix</span><span class="p">[</span><span class="mi">0</span><span class="p">])),</span>
            <span class="s2">&quot;      B : &quot;</span> <span class="o">+</span> <span class="s2">&quot; &quot;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="nb">map</span><span class="p">(</span><span class="nb">repr</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">_matrix</span><span class="p">[</span><span class="mi">1</span><span class="p">])),</span>
            <span class="s2">&quot;      C : &quot;</span> <span class="o">+</span> <span class="s2">&quot; &quot;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="nb">map</span><span class="p">(</span><span class="nb">repr</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">_matrix</span><span class="p">[</span><span class="mi">2</span><span class="p">])),</span>
        <span class="p">]</span>
        <span class="k">return</span> <span class="s2">&quot;</span><span class="se">\n</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">outs</span><span class="p">)</span>

    <span class="k">def</span> <span class="fm">__eq__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">other</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        A lattice is considered to be equal to another if the internal matrix</span>
<span class="sd">        representation satisfies np.allclose(matrix1, matrix2) to be True.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="n">other</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">return</span> <span class="kc">False</span>
        <span class="c1"># shortcut the np.allclose if the memory addresses are the same</span>
        <span class="c1"># (very common in Structure.from_sites)</span>
        <span class="k">return</span> <span class="bp">self</span> <span class="ow">is</span> <span class="n">other</span> <span class="ow">or</span> <span class="n">np</span><span class="o">.</span><span class="n">allclose</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">matrix</span><span class="p">,</span> <span class="n">other</span><span class="o">.</span><span class="n">matrix</span><span class="p">)</span>

    <span class="k">def</span> <span class="fm">__ne__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">other</span><span class="p">):</span>
        <span class="k">return</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="fm">__eq__</span><span class="p">(</span><span class="n">other</span><span class="p">)</span>

    <span class="k">def</span> <span class="fm">__hash__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="mi">7</span>

    <span class="k">def</span> <span class="fm">__str__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="s2">&quot;</span><span class="se">\n</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">join</span><span class="p">([</span><span class="s2">&quot; &quot;</span><span class="o">.</span><span class="n">join</span><span class="p">([</span><span class="s2">&quot;</span><span class="si">%.6f</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="n">i</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">row</span><span class="p">])</span> <span class="k">for</span> <span class="n">row</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_matrix</span><span class="p">])</span>

<div class="viewcode-block" id="Lattice.as_dict"><a class="viewcode-back" href="../../../pymatgen.core.lattice.html#pymatgen.core.lattice.Lattice.as_dict">[docs]</a>    <span class="k">def</span> <span class="nf">as_dict</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">verbosity</span><span class="p">:</span> <span class="nb">int</span> <span class="o">=</span> <span class="mi">0</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">Dict</span><span class="p">:</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Json-serialization dict representation of the Lattice.</span>

<span class="sd">        Args:</span>
<span class="sd">            verbosity (int): Verbosity level. Default of 0 only includes the</span>
<span class="sd">                matrix representation. Set to 1 for more details.</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="n">d</span> <span class="o">=</span> <span class="p">{</span>
            <span class="s2">&quot;@module&quot;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="vm">__module__</span><span class="p">,</span>
            <span class="s2">&quot;@class&quot;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="vm">__name__</span><span class="p">,</span>
            <span class="s2">&quot;matrix&quot;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_matrix</span><span class="o">.</span><span class="n">tolist</span><span class="p">(),</span>
        <span class="p">}</span>
        <span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">,</span> <span class="n">c</span><span class="p">,</span> <span class="n">alpha</span><span class="p">,</span> <span class="n">beta</span><span class="p">,</span> <span class="n">gamma</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">parameters</span>
        <span class="k">if</span> <span class="n">verbosity</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">:</span>
            <span class="n">d</span><span class="o">.</span><span class="n">update</span><span class="p">(</span>
                <span class="p">{</span>
                    <span class="s2">&quot;a&quot;</span><span class="p">:</span> <span class="n">a</span><span class="p">,</span>
                    <span class="s2">&quot;b&quot;</span><span class="p">:</span> <span class="n">b</span><span class="p">,</span>
                    <span class="s2">&quot;c&quot;</span><span class="p">:</span> <span class="n">c</span><span class="p">,</span>
                    <span class="s2">&quot;alpha&quot;</span><span class="p">:</span> <span class="n">alpha</span><span class="p">,</span>
                    <span class="s2">&quot;beta&quot;</span><span class="p">:</span> <span class="n">beta</span><span class="p">,</span>
                    <span class="s2">&quot;gamma&quot;</span><span class="p">:</span> <span class="n">gamma</span><span class="p">,</span>
                    <span class="s2">&quot;volume&quot;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">volume</span><span class="p">,</span>
                <span class="p">}</span>
            <span class="p">)</span>

        <span class="k">return</span> <span class="n">d</span></div>

<div class="viewcode-block" id="Lattice.find_all_mappings"><a class="viewcode-back" href="../../../pymatgen.core.lattice.html#pymatgen.core.lattice.Lattice.find_all_mappings">[docs]</a>    <span class="k">def</span> <span class="nf">find_all_mappings</span><span class="p">(</span>
            <span class="bp">self</span><span class="p">,</span>
            <span class="n">other_lattice</span><span class="p">:</span> <span class="s2">&quot;Lattice&quot;</span><span class="p">,</span>
            <span class="n">ltol</span><span class="p">:</span> <span class="nb">float</span> <span class="o">=</span> <span class="mf">1e-5</span><span class="p">,</span>
            <span class="n">atol</span><span class="p">:</span> <span class="nb">float</span> <span class="o">=</span> <span class="mi">1</span><span class="p">,</span>
            <span class="n">skip_rotation_matrix</span><span class="p">:</span> <span class="nb">bool</span> <span class="o">=</span> <span class="kc">False</span><span class="p">,</span>
    <span class="p">)</span> <span class="o">-&gt;</span> <span class="n">Iterator</span><span class="p">[</span><span class="n">Tuple</span><span class="p">[</span><span class="s2">&quot;Lattice&quot;</span><span class="p">,</span> <span class="n">Optional</span><span class="p">[</span><span class="n">np</span><span class="o">.</span><span class="n">ndarray</span><span class="p">],</span> <span class="n">np</span><span class="o">.</span><span class="n">ndarray</span><span class="p">]]:</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Finds all mappings between current lattice and another lattice.</span>

<span class="sd">        Args:</span>
<span class="sd">            other_lattice (Lattice): Another lattice that is equivalent to</span>
<span class="sd">                this one.</span>
<span class="sd">            ltol (float): Tolerance for matching lengths. Defaults to 1e-5.</span>
<span class="sd">            atol (float): Tolerance for matching angles. Defaults to 1.</span>
<span class="sd">            skip_rotation_matrix (bool): Whether to skip calculation of the</span>
<span class="sd">                rotation matrix</span>

<span class="sd">        Yields:</span>
<span class="sd">            (aligned_lattice, rotation_matrix, scale_matrix) if a mapping is</span>
<span class="sd">            found. aligned_lattice is a rotated version of other_lattice that</span>
<span class="sd">            has the same lattice parameters, but which is aligned in the</span>
<span class="sd">            coordinate system of this lattice so that translational points</span>
<span class="sd">            match up in 3D. rotation_matrix is the rotation that has to be</span>
<span class="sd">            applied to other_lattice to obtain aligned_lattice, i.e.,</span>
<span class="sd">            aligned_matrix = np.inner(other_lattice, rotation_matrix) and</span>
<span class="sd">            op = SymmOp.from_rotation_and_translation(rotation_matrix)</span>
<span class="sd">            aligned_matrix = op.operate_multi(latt.matrix)</span>
<span class="sd">            Finally, scale_matrix is the integer matrix that expresses</span>
<span class="sd">            aligned_matrix as a linear combination of this</span>
<span class="sd">            lattice, i.e., aligned_matrix = np.dot(scale_matrix, self.matrix)</span>

<span class="sd">            None is returned if no matches are found.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">lengths</span> <span class="o">=</span> <span class="n">other_lattice</span><span class="o">.</span><span class="n">lengths</span>
        <span class="p">(</span><span class="n">alpha</span><span class="p">,</span> <span class="n">beta</span><span class="p">,</span> <span class="n">gamma</span><span class="p">)</span> <span class="o">=</span> <span class="n">other_lattice</span><span class="o">.</span><span class="n">angles</span>

        <span class="n">frac</span><span class="p">,</span> <span class="n">dist</span><span class="p">,</span> <span class="n">_</span><span class="p">,</span> <span class="n">_</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_points_in_sphere</span><span class="p">(</span>
            <span class="p">[[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">]],</span> <span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">],</span> <span class="nb">max</span><span class="p">(</span><span class="n">lengths</span><span class="p">)</span> <span class="o">*</span> <span class="p">(</span><span class="mi">1</span> <span class="o">+</span> <span class="n">ltol</span><span class="p">),</span> <span class="n">zip_results</span><span class="o">=</span><span class="kc">False</span>
        <span class="p">)</span>
        <span class="n">cart</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_cartesian_coords</span><span class="p">(</span><span class="n">frac</span><span class="p">)</span>
        <span class="c1"># this can&#39;t be broadcast because they&#39;re different lengths</span>
        <span class="n">inds</span> <span class="o">=</span> <span class="p">[</span>
            <span class="n">np</span><span class="o">.</span><span class="n">logical_and</span><span class="p">(</span><span class="n">dist</span> <span class="o">/</span> <span class="n">l</span> <span class="o">&lt;</span> <span class="mi">1</span> <span class="o">+</span> <span class="n">ltol</span><span class="p">,</span> <span class="n">dist</span> <span class="o">/</span> <span class="n">l</span> <span class="o">&gt;</span> <span class="mi">1</span> <span class="o">/</span> <span class="p">(</span><span class="mi">1</span> <span class="o">+</span> <span class="n">ltol</span><span class="p">))</span>  <span class="c1"># type: ignore</span>
            <span class="k">for</span> <span class="n">l</span> <span class="ow">in</span> <span class="n">lengths</span>
        <span class="p">]</span>
        <span class="n">c_a</span><span class="p">,</span> <span class="n">c_b</span><span class="p">,</span> <span class="n">c_c</span> <span class="o">=</span> <span class="p">(</span><span class="n">cart</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">inds</span><span class="p">)</span>
        <span class="n">f_a</span><span class="p">,</span> <span class="n">f_b</span><span class="p">,</span> <span class="n">f_c</span> <span class="o">=</span> <span class="p">(</span><span class="n">frac</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">inds</span><span class="p">)</span>
        <span class="n">l_a</span><span class="p">,</span> <span class="n">l_b</span><span class="p">,</span> <span class="n">l_c</span> <span class="o">=</span> <span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">sum</span><span class="p">(</span><span class="n">c</span> <span class="o">**</span> <span class="mi">2</span><span class="p">,</span> <span class="n">axis</span><span class="o">=-</span><span class="mi">1</span><span class="p">)</span> <span class="o">**</span> <span class="mf">0.5</span> <span class="k">for</span> <span class="n">c</span> <span class="ow">in</span> <span class="p">(</span><span class="n">c_a</span><span class="p">,</span> <span class="n">c_b</span><span class="p">,</span> <span class="n">c_c</span><span class="p">))</span>

        <span class="k">def</span> <span class="nf">get_angles</span><span class="p">(</span><span class="n">v1</span><span class="p">,</span> <span class="n">v2</span><span class="p">,</span> <span class="n">l1</span><span class="p">,</span> <span class="n">l2</span><span class="p">):</span>
            <span class="n">x</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">inner</span><span class="p">(</span><span class="n">v1</span><span class="p">,</span> <span class="n">v2</span><span class="p">)</span> <span class="o">/</span> <span class="n">l1</span><span class="p">[:,</span> <span class="kc">None</span><span class="p">]</span> <span class="o">/</span> <span class="n">l2</span>
            <span class="n">x</span><span class="p">[</span><span class="n">x</span> <span class="o">&gt;</span> <span class="mi">1</span><span class="p">]</span> <span class="o">=</span> <span class="mi">1</span>
            <span class="n">x</span><span class="p">[</span><span class="n">x</span> <span class="o">&lt;</span> <span class="o">-</span><span class="mi">1</span><span class="p">]</span> <span class="o">=</span> <span class="o">-</span><span class="mi">1</span>
            <span class="n">angles</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">arccos</span><span class="p">(</span><span class="n">x</span><span class="p">)</span> <span class="o">*</span> <span class="mf">180.0</span> <span class="o">/</span> <span class="n">pi</span>
            <span class="k">return</span> <span class="n">angles</span>

        <span class="n">alphab</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">abs</span><span class="p">(</span><span class="n">get_angles</span><span class="p">(</span><span class="n">c_b</span><span class="p">,</span> <span class="n">c_c</span><span class="p">,</span> <span class="n">l_b</span><span class="p">,</span> <span class="n">l_c</span><span class="p">)</span> <span class="o">-</span> <span class="n">alpha</span><span class="p">)</span> <span class="o">&lt;</span> <span class="n">atol</span>
        <span class="n">betab</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">abs</span><span class="p">(</span><span class="n">get_angles</span><span class="p">(</span><span class="n">c_a</span><span class="p">,</span> <span class="n">c_c</span><span class="p">,</span> <span class="n">l_a</span><span class="p">,</span> <span class="n">l_c</span><span class="p">)</span> <span class="o">-</span> <span class="n">beta</span><span class="p">)</span> <span class="o">&lt;</span> <span class="n">atol</span>
        <span class="n">gammab</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">abs</span><span class="p">(</span><span class="n">get_angles</span><span class="p">(</span><span class="n">c_a</span><span class="p">,</span> <span class="n">c_b</span><span class="p">,</span> <span class="n">l_a</span><span class="p">,</span> <span class="n">l_b</span><span class="p">)</span> <span class="o">-</span> <span class="n">gamma</span><span class="p">)</span> <span class="o">&lt;</span> <span class="n">atol</span>

        <span class="k">for</span> <span class="n">i</span><span class="p">,</span> <span class="n">all_j</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">gammab</span><span class="p">):</span>
            <span class="n">inds</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">logical_and</span><span class="p">(</span>
                <span class="n">all_j</span><span class="p">[:,</span> <span class="kc">None</span><span class="p">],</span> <span class="n">np</span><span class="o">.</span><span class="n">logical_and</span><span class="p">(</span><span class="n">alphab</span><span class="p">,</span> <span class="n">betab</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="kc">None</span><span class="p">,</span> <span class="p">:])</span>
            <span class="p">)</span>
            <span class="k">for</span> <span class="n">j</span><span class="p">,</span> <span class="n">k</span> <span class="ow">in</span> <span class="n">np</span><span class="o">.</span><span class="n">argwhere</span><span class="p">(</span><span class="n">inds</span><span class="p">):</span>
                <span class="n">scale_m</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">((</span><span class="n">f_a</span><span class="p">[</span><span class="n">i</span><span class="p">],</span> <span class="n">f_b</span><span class="p">[</span><span class="n">j</span><span class="p">],</span> <span class="n">f_c</span><span class="p">[</span><span class="n">k</span><span class="p">]),</span> <span class="n">dtype</span><span class="o">=</span><span class="n">np</span><span class="o">.</span><span class="n">int</span><span class="p">)</span>  <span class="c1"># type: ignore</span>
                <span class="k">if</span> <span class="nb">abs</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">linalg</span><span class="o">.</span><span class="n">det</span><span class="p">(</span><span class="n">scale_m</span><span class="p">))</span> <span class="o">&lt;</span> <span class="mf">1e-8</span><span class="p">:</span>
                    <span class="k">continue</span>

                <span class="n">aligned_m</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">((</span><span class="n">c_a</span><span class="p">[</span><span class="n">i</span><span class="p">],</span> <span class="n">c_b</span><span class="p">[</span><span class="n">j</span><span class="p">],</span> <span class="n">c_c</span><span class="p">[</span><span class="n">k</span><span class="p">]))</span>

                <span class="k">if</span> <span class="n">skip_rotation_matrix</span><span class="p">:</span>
                    <span class="n">rotation_m</span> <span class="o">=</span> <span class="kc">None</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="n">rotation_m</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">linalg</span><span class="o">.</span><span class="n">solve</span><span class="p">(</span><span class="n">aligned_m</span><span class="p">,</span> <span class="n">other_lattice</span><span class="o">.</span><span class="n">matrix</span><span class="p">)</span>

                <span class="k">yield</span> <span class="n">Lattice</span><span class="p">(</span><span class="n">aligned_m</span><span class="p">),</span> <span class="n">rotation_m</span><span class="p">,</span> <span class="n">scale_m</span></div>

<div class="viewcode-block" id="Lattice.find_mapping"><a class="viewcode-back" href="../../../pymatgen.core.lattice.html#pymatgen.core.lattice.Lattice.find_mapping">[docs]</a>    <span class="k">def</span> <span class="nf">find_mapping</span><span class="p">(</span>
            <span class="bp">self</span><span class="p">,</span>
            <span class="n">other_lattice</span><span class="p">:</span> <span class="s2">&quot;Lattice&quot;</span><span class="p">,</span>
            <span class="n">ltol</span><span class="p">:</span> <span class="nb">float</span> <span class="o">=</span> <span class="mf">1e-5</span><span class="p">,</span>
            <span class="n">atol</span><span class="p">:</span> <span class="nb">float</span> <span class="o">=</span> <span class="mi">1</span><span class="p">,</span>
            <span class="n">skip_rotation_matrix</span><span class="p">:</span> <span class="nb">bool</span> <span class="o">=</span> <span class="kc">False</span><span class="p">,</span>
    <span class="p">)</span> <span class="o">-&gt;</span> <span class="n">Optional</span><span class="p">[</span><span class="n">Tuple</span><span class="p">[</span><span class="s2">&quot;Lattice&quot;</span><span class="p">,</span> <span class="n">Optional</span><span class="p">[</span><span class="n">np</span><span class="o">.</span><span class="n">ndarray</span><span class="p">],</span> <span class="n">np</span><span class="o">.</span><span class="n">ndarray</span><span class="p">]]:</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Finds a mapping between current lattice and another lattice. There</span>
<span class="sd">        are an infinite number of choices of basis vectors for two entirely</span>
<span class="sd">        equivalent lattices. This method returns a mapping that maps</span>
<span class="sd">        other_lattice to this lattice.</span>

<span class="sd">        Args:</span>
<span class="sd">            other_lattice (Lattice): Another lattice that is equivalent to</span>
<span class="sd">                this one.</span>
<span class="sd">            ltol (float): Tolerance for matching lengths. Defaults to 1e-5.</span>
<span class="sd">            atol (float): Tolerance for matching angles. Defaults to 1.</span>

<span class="sd">        Returns:</span>
<span class="sd">            (aligned_lattice, rotation_matrix, scale_matrix) if a mapping is</span>
<span class="sd">            found. aligned_lattice is a rotated version of other_lattice that</span>
<span class="sd">            has the same lattice parameters, but which is aligned in the</span>
<span class="sd">            coordinate system of this lattice so that translational points</span>
<span class="sd">            match up in 3D. rotation_matrix is the rotation that has to be</span>
<span class="sd">            applied to other_lattice to obtain aligned_lattice, i.e.,</span>
<span class="sd">            aligned_matrix = np.inner(other_lattice, rotation_matrix) and</span>
<span class="sd">            op = SymmOp.from_rotation_and_translation(rotation_matrix)</span>
<span class="sd">            aligned_matrix = op.operate_multi(latt.matrix)</span>
<span class="sd">            Finally, scale_matrix is the integer matrix that expresses</span>
<span class="sd">            aligned_matrix as a linear combination of this</span>
<span class="sd">            lattice, i.e., aligned_matrix = np.dot(scale_matrix, self.matrix)</span>

<span class="sd">            None is returned if no matches are found.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">find_all_mappings</span><span class="p">(</span>
                <span class="n">other_lattice</span><span class="p">,</span> <span class="n">ltol</span><span class="p">,</span> <span class="n">atol</span><span class="p">,</span> <span class="n">skip_rotation_matrix</span><span class="o">=</span><span class="n">skip_rotation_matrix</span>
        <span class="p">):</span>
            <span class="k">return</span> <span class="n">x</span>
        <span class="k">return</span> <span class="kc">None</span></div>

<div class="viewcode-block" id="Lattice.get_lll_reduced_lattice"><a class="viewcode-back" href="../../../pymatgen.core.lattice.html#pymatgen.core.lattice.Lattice.get_lll_reduced_lattice">[docs]</a>    <span class="k">def</span> <span class="nf">get_lll_reduced_lattice</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">delta</span><span class="p">:</span> <span class="nb">float</span> <span class="o">=</span> <span class="mf">0.75</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="s2">&quot;Lattice&quot;</span><span class="p">:</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        :param delta: Delta parameter.</span>
<span class="sd">        :return: LLL reduced Lattice.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="n">delta</span> <span class="ow">not</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_lll_matrix_mappings</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_lll_matrix_mappings</span><span class="p">[</span><span class="n">delta</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_calculate_lll</span><span class="p">()</span>
        <span class="k">return</span> <span class="n">Lattice</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_lll_matrix_mappings</span><span class="p">[</span><span class="n">delta</span><span class="p">][</span><span class="mi">0</span><span class="p">])</span></div>

    <span class="k">def</span> <span class="nf">_calculate_lll</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">delta</span><span class="p">:</span> <span class="nb">float</span> <span class="o">=</span> <span class="mf">0.75</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">Tuple</span><span class="p">[</span><span class="n">np</span><span class="o">.</span><span class="n">ndarray</span><span class="p">,</span> <span class="n">np</span><span class="o">.</span><span class="n">ndarray</span><span class="p">]:</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Performs a Lenstra-Lenstra-Lovasz lattice basis reduction to obtain a</span>
<span class="sd">        c-reduced basis. This method returns a basis which is as &quot;good&quot; as</span>
<span class="sd">        possible, with &quot;good&quot; defined by orthongonality of the lattice vectors.</span>

<span class="sd">        This basis is used for all the periodic boundary condition calculations.</span>

<span class="sd">        Args:</span>
<span class="sd">            delta (float): Reduction parameter. Default of 0.75 is usually</span>
<span class="sd">                fine.</span>

<span class="sd">        Returns:</span>
<span class="sd">            Reduced lattice matrix, mapping to get to that lattice.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="c1"># Transpose the lattice matrix first so that basis vectors are columns.</span>
        <span class="c1"># Makes life easier.</span>
        <span class="n">a</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_matrix</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span><span class="o">.</span><span class="n">T</span>

        <span class="n">b</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">((</span><span class="mi">3</span><span class="p">,</span> <span class="mi">3</span><span class="p">))</span>  <span class="c1"># Vectors after the Gram-Schmidt process</span>
        <span class="n">u</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">((</span><span class="mi">3</span><span class="p">,</span> <span class="mi">3</span><span class="p">))</span>  <span class="c1"># Gram-Schmidt coeffieicnts</span>
        <span class="n">m</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">(</span><span class="mi">3</span><span class="p">)</span>  <span class="c1"># These are the norm squared of each vec.</span>

        <span class="n">b</span><span class="p">[:,</span> <span class="mi">0</span><span class="p">]</span> <span class="o">=</span> <span class="n">a</span><span class="p">[:,</span> <span class="mi">0</span><span class="p">]</span>
        <span class="n">m</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">=</span> <span class="n">dot</span><span class="p">(</span><span class="n">b</span><span class="p">[:,</span> <span class="mi">0</span><span class="p">],</span> <span class="n">b</span><span class="p">[:,</span> <span class="mi">0</span><span class="p">])</span>
        <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">3</span><span class="p">):</span>
            <span class="n">u</span><span class="p">[</span><span class="n">i</span><span class="p">,</span> <span class="mi">0</span><span class="p">:</span><span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="n">dot</span><span class="p">(</span><span class="n">a</span><span class="p">[:,</span> <span class="n">i</span><span class="p">]</span><span class="o">.</span><span class="n">T</span><span class="p">,</span> <span class="n">b</span><span class="p">[:,</span> <span class="mi">0</span><span class="p">:</span><span class="n">i</span><span class="p">])</span> <span class="o">/</span> <span class="n">m</span><span class="p">[</span><span class="mi">0</span><span class="p">:</span><span class="n">i</span><span class="p">]</span>
            <span class="n">b</span><span class="p">[:,</span> <span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="n">a</span><span class="p">[:,</span> <span class="n">i</span><span class="p">]</span> <span class="o">-</span> <span class="n">dot</span><span class="p">(</span><span class="n">b</span><span class="p">[:,</span> <span class="mi">0</span><span class="p">:</span><span class="n">i</span><span class="p">],</span> <span class="n">u</span><span class="p">[</span><span class="n">i</span><span class="p">,</span> <span class="mi">0</span><span class="p">:</span><span class="n">i</span><span class="p">]</span><span class="o">.</span><span class="n">T</span><span class="p">)</span>
            <span class="n">m</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="n">dot</span><span class="p">(</span><span class="n">b</span><span class="p">[:,</span> <span class="n">i</span><span class="p">],</span> <span class="n">b</span><span class="p">[:,</span> <span class="n">i</span><span class="p">])</span>

        <span class="n">k</span> <span class="o">=</span> <span class="mi">2</span>

        <span class="n">mapping</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">identity</span><span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="n">dtype</span><span class="o">=</span><span class="n">np</span><span class="o">.</span><span class="n">double</span><span class="p">)</span>
        <span class="k">while</span> <span class="n">k</span> <span class="o">&lt;=</span> <span class="mi">3</span><span class="p">:</span>
            <span class="c1"># Size reduction.</span>
            <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">k</span> <span class="o">-</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="o">-</span><span class="mi">1</span><span class="p">):</span>
                <span class="n">q</span> <span class="o">=</span> <span class="nb">round</span><span class="p">(</span><span class="n">u</span><span class="p">[</span><span class="n">k</span> <span class="o">-</span> <span class="mi">1</span><span class="p">,</span> <span class="n">i</span> <span class="o">-</span> <span class="mi">1</span><span class="p">])</span>
                <span class="k">if</span> <span class="n">q</span> <span class="o">!=</span> <span class="mi">0</span><span class="p">:</span>
                    <span class="c1"># Reduce the k-th basis vector.</span>
                    <span class="n">a</span><span class="p">[:,</span> <span class="n">k</span> <span class="o">-</span> <span class="mi">1</span><span class="p">]</span> <span class="o">=</span> <span class="n">a</span><span class="p">[:,</span> <span class="n">k</span> <span class="o">-</span> <span class="mi">1</span><span class="p">]</span> <span class="o">-</span> <span class="n">q</span> <span class="o">*</span> <span class="n">a</span><span class="p">[:,</span> <span class="n">i</span> <span class="o">-</span> <span class="mi">1</span><span class="p">]</span>
                    <span class="n">mapping</span><span class="p">[:,</span> <span class="n">k</span> <span class="o">-</span> <span class="mi">1</span><span class="p">]</span> <span class="o">=</span> <span class="n">mapping</span><span class="p">[:,</span> <span class="n">k</span> <span class="o">-</span> <span class="mi">1</span><span class="p">]</span> <span class="o">-</span> <span class="n">q</span> <span class="o">*</span> <span class="n">mapping</span><span class="p">[:,</span> <span class="n">i</span> <span class="o">-</span> <span class="mi">1</span><span class="p">]</span>
                    <span class="n">uu</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="n">u</span><span class="p">[</span><span class="n">i</span> <span class="o">-</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">:</span> <span class="p">(</span><span class="n">i</span> <span class="o">-</span> <span class="mi">1</span><span class="p">)])</span>
                    <span class="n">uu</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
                    <span class="c1"># Update the GS coefficients.</span>
                    <span class="n">u</span><span class="p">[</span><span class="n">k</span> <span class="o">-</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">:</span><span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="n">u</span><span class="p">[</span><span class="n">k</span> <span class="o">-</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">:</span><span class="n">i</span><span class="p">]</span> <span class="o">-</span> <span class="n">q</span> <span class="o">*</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">uu</span><span class="p">)</span>

            <span class="c1"># Check the Lovasz condition.</span>
            <span class="k">if</span> <span class="n">dot</span><span class="p">(</span><span class="n">b</span><span class="p">[:,</span> <span class="n">k</span> <span class="o">-</span> <span class="mi">1</span><span class="p">],</span> <span class="n">b</span><span class="p">[:,</span> <span class="n">k</span> <span class="o">-</span> <span class="mi">1</span><span class="p">])</span> <span class="o">&gt;=</span> <span class="p">(</span>
                    <span class="n">delta</span> <span class="o">-</span> <span class="nb">abs</span><span class="p">(</span><span class="n">u</span><span class="p">[</span><span class="n">k</span> <span class="o">-</span> <span class="mi">1</span><span class="p">,</span> <span class="n">k</span> <span class="o">-</span> <span class="mi">2</span><span class="p">])</span> <span class="o">**</span> <span class="mi">2</span>
            <span class="p">)</span> <span class="o">*</span> <span class="n">dot</span><span class="p">(</span><span class="n">b</span><span class="p">[:,</span> <span class="p">(</span><span class="n">k</span> <span class="o">-</span> <span class="mi">2</span><span class="p">)],</span> <span class="n">b</span><span class="p">[:,</span> <span class="p">(</span><span class="n">k</span> <span class="o">-</span> <span class="mi">2</span><span class="p">)]):</span>
                <span class="c1"># Increment k if the Lovasz condition holds.</span>
                <span class="n">k</span> <span class="o">+=</span> <span class="mi">1</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="c1"># If the Lovasz condition fails,</span>
                <span class="c1"># swap the k-th and (k-1)-th basis vector</span>
                <span class="n">v</span> <span class="o">=</span> <span class="n">a</span><span class="p">[:,</span> <span class="n">k</span> <span class="o">-</span> <span class="mi">1</span><span class="p">]</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span>
                <span class="n">a</span><span class="p">[:,</span> <span class="n">k</span> <span class="o">-</span> <span class="mi">1</span><span class="p">]</span> <span class="o">=</span> <span class="n">a</span><span class="p">[:,</span> <span class="n">k</span> <span class="o">-</span> <span class="mi">2</span><span class="p">]</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span>
                <span class="n">a</span><span class="p">[:,</span> <span class="n">k</span> <span class="o">-</span> <span class="mi">2</span><span class="p">]</span> <span class="o">=</span> <span class="n">v</span>

                <span class="n">v_m</span> <span class="o">=</span> <span class="n">mapping</span><span class="p">[:,</span> <span class="n">k</span> <span class="o">-</span> <span class="mi">1</span><span class="p">]</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span>
                <span class="n">mapping</span><span class="p">[:,</span> <span class="n">k</span> <span class="o">-</span> <span class="mi">1</span><span class="p">]</span> <span class="o">=</span> <span class="n">mapping</span><span class="p">[:,</span> <span class="n">k</span> <span class="o">-</span> <span class="mi">2</span><span class="p">]</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span>
                <span class="n">mapping</span><span class="p">[:,</span> <span class="n">k</span> <span class="o">-</span> <span class="mi">2</span><span class="p">]</span> <span class="o">=</span> <span class="n">v_m</span>

                <span class="c1"># Update the Gram-Schmidt coefficients</span>
                <span class="k">for</span> <span class="n">s</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">k</span> <span class="o">-</span> <span class="mi">1</span><span class="p">,</span> <span class="n">k</span> <span class="o">+</span> <span class="mi">1</span><span class="p">):</span>
                    <span class="n">u</span><span class="p">[</span><span class="n">s</span> <span class="o">-</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">:</span> <span class="p">(</span><span class="n">s</span> <span class="o">-</span> <span class="mi">1</span><span class="p">)]</span> <span class="o">=</span> <span class="p">(</span>
                        <span class="n">dot</span><span class="p">(</span><span class="n">a</span><span class="p">[:,</span> <span class="n">s</span> <span class="o">-</span> <span class="mi">1</span><span class="p">]</span><span class="o">.</span><span class="n">T</span><span class="p">,</span> <span class="n">b</span><span class="p">[:,</span> <span class="mi">0</span><span class="p">:</span> <span class="p">(</span><span class="n">s</span> <span class="o">-</span> <span class="mi">1</span><span class="p">)])</span> <span class="o">/</span> <span class="n">m</span><span class="p">[</span><span class="mi">0</span><span class="p">:</span> <span class="p">(</span><span class="n">s</span> <span class="o">-</span> <span class="mi">1</span><span class="p">)]</span>
                    <span class="p">)</span>
                    <span class="n">b</span><span class="p">[:,</span> <span class="n">s</span> <span class="o">-</span> <span class="mi">1</span><span class="p">]</span> <span class="o">=</span> <span class="n">a</span><span class="p">[:,</span> <span class="n">s</span> <span class="o">-</span> <span class="mi">1</span><span class="p">]</span> <span class="o">-</span> <span class="n">dot</span><span class="p">(</span>
                        <span class="n">b</span><span class="p">[:,</span> <span class="mi">0</span><span class="p">:</span> <span class="p">(</span><span class="n">s</span> <span class="o">-</span> <span class="mi">1</span><span class="p">)],</span> <span class="n">u</span><span class="p">[</span><span class="n">s</span> <span class="o">-</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">:</span> <span class="p">(</span><span class="n">s</span> <span class="o">-</span> <span class="mi">1</span><span class="p">)]</span><span class="o">.</span><span class="n">T</span>
                    <span class="p">)</span>
                    <span class="n">m</span><span class="p">[</span><span class="n">s</span> <span class="o">-</span> <span class="mi">1</span><span class="p">]</span> <span class="o">=</span> <span class="n">dot</span><span class="p">(</span><span class="n">b</span><span class="p">[:,</span> <span class="n">s</span> <span class="o">-</span> <span class="mi">1</span><span class="p">],</span> <span class="n">b</span><span class="p">[:,</span> <span class="n">s</span> <span class="o">-</span> <span class="mi">1</span><span class="p">])</span>

                <span class="k">if</span> <span class="n">k</span> <span class="o">&gt;</span> <span class="mi">2</span><span class="p">:</span>
                    <span class="n">k</span> <span class="o">-=</span> <span class="mi">1</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="c1"># We have to do p/q, so do lstsq(q.T, p.T).T instead.</span>
                    <span class="n">p</span> <span class="o">=</span> <span class="n">dot</span><span class="p">(</span><span class="n">a</span><span class="p">[:,</span> <span class="n">k</span><span class="p">:</span><span class="mi">3</span><span class="p">]</span><span class="o">.</span><span class="n">T</span><span class="p">,</span> <span class="n">b</span><span class="p">[:,</span> <span class="p">(</span><span class="n">k</span> <span class="o">-</span> <span class="mi">2</span><span class="p">):</span> <span class="n">k</span><span class="p">])</span>
                    <span class="n">q</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">diag</span><span class="p">(</span><span class="n">m</span><span class="p">[(</span><span class="n">k</span> <span class="o">-</span> <span class="mi">2</span><span class="p">):</span> <span class="n">k</span><span class="p">])</span>
                    <span class="n">result</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">linalg</span><span class="o">.</span><span class="n">lstsq</span><span class="p">(</span><span class="n">q</span><span class="o">.</span><span class="n">T</span><span class="p">,</span> <span class="n">p</span><span class="o">.</span><span class="n">T</span><span class="p">,</span> <span class="n">rcond</span><span class="o">=</span><span class="kc">None</span><span class="p">)[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">T</span>  <span class="c1"># type: ignore</span>
                    <span class="n">u</span><span class="p">[</span><span class="n">k</span><span class="p">:</span><span class="mi">3</span><span class="p">,</span> <span class="p">(</span><span class="n">k</span> <span class="o">-</span> <span class="mi">2</span><span class="p">):</span> <span class="n">k</span><span class="p">]</span> <span class="o">=</span> <span class="n">result</span>

        <span class="k">return</span> <span class="n">a</span><span class="o">.</span><span class="n">T</span><span class="p">,</span> <span class="n">mapping</span><span class="o">.</span><span class="n">T</span>

<div class="viewcode-block" id="Lattice.get_lll_frac_coords"><a class="viewcode-back" href="../../../pymatgen.core.lattice.html#pymatgen.core.lattice.Lattice.get_lll_frac_coords">[docs]</a>    <span class="k">def</span> <span class="nf">get_lll_frac_coords</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">frac_coords</span><span class="p">:</span> <span class="n">Vector3Like</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">np</span><span class="o">.</span><span class="n">ndarray</span><span class="p">:</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Given fractional coordinates in the lattice basis, returns corresponding</span>
<span class="sd">        fractional coordinates in the lll basis.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="n">dot</span><span class="p">(</span><span class="n">frac_coords</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">lll_inverse</span><span class="p">)</span></div>

<div class="viewcode-block" id="Lattice.get_frac_coords_from_lll"><a class="viewcode-back" href="../../../pymatgen.core.lattice.html#pymatgen.core.lattice.Lattice.get_frac_coords_from_lll">[docs]</a>    <span class="k">def</span> <span class="nf">get_frac_coords_from_lll</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">lll_frac_coords</span><span class="p">:</span> <span class="n">Vector3Like</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">np</span><span class="o">.</span><span class="n">ndarray</span><span class="p">:</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Given fractional coordinates in the lll basis, returns corresponding</span>
<span class="sd">        fractional coordinates in the lattice basis.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="n">dot</span><span class="p">(</span><span class="n">lll_frac_coords</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">lll_mapping</span><span class="p">)</span></div>

<div class="viewcode-block" id="Lattice.get_niggli_reduced_lattice"><a class="viewcode-back" href="../../../pymatgen.core.lattice.html#pymatgen.core.lattice.Lattice.get_niggli_reduced_lattice">[docs]</a>    <span class="k">def</span> <span class="nf">get_niggli_reduced_lattice</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">tol</span><span class="p">:</span> <span class="nb">float</span> <span class="o">=</span> <span class="mf">1e-5</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="s2">&quot;Lattice&quot;</span><span class="p">:</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Get the Niggli reduced lattice using the numerically stable algo</span>
<span class="sd">        proposed by R. W. Grosse-Kunstleve, N. K. Sauter, &amp; P. D. Adams,</span>
<span class="sd">        Acta Crystallographica Section A Foundations of Crystallography, 2003,</span>
<span class="sd">        60(1), 1-6. doi:10.1107/S010876730302186X</span>

<span class="sd">        Args:</span>
<span class="sd">            tol (float): The numerical tolerance. The default of 1e-5 should</span>
<span class="sd">                result in stable behavior for most cases.</span>

<span class="sd">        Returns:</span>
<span class="sd">            Niggli-reduced lattice.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="c1"># lll reduction is more stable for skewed cells</span>
        <span class="n">matrix</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">lll_matrix</span>
        <span class="n">e</span> <span class="o">=</span> <span class="n">tol</span> <span class="o">*</span> <span class="bp">self</span><span class="o">.</span><span class="n">volume</span> <span class="o">**</span> <span class="p">(</span><span class="mi">1</span> <span class="o">/</span> <span class="mi">3</span><span class="p">)</span>

        <span class="c1"># Define metric tensor</span>
        <span class="n">G</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">dot</span><span class="p">(</span><span class="n">matrix</span><span class="p">,</span> <span class="n">matrix</span><span class="o">.</span><span class="n">T</span><span class="p">)</span>

        <span class="c1"># This sets an upper limit on the number of iterations.</span>
        <span class="k">for</span> <span class="n">count</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">100</span><span class="p">):</span>
            <span class="c1"># The steps are labelled as Ax as per the labelling scheme in the</span>
            <span class="c1"># paper.</span>
            <span class="p">(</span><span class="n">A</span><span class="p">,</span> <span class="n">B</span><span class="p">,</span> <span class="n">C</span><span class="p">,</span> <span class="n">E</span><span class="p">,</span> <span class="n">N</span><span class="p">,</span> <span class="n">Y</span><span class="p">)</span> <span class="o">=</span> <span class="p">(</span>
                <span class="n">G</span><span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">],</span>
                <span class="n">G</span><span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">],</span>
                <span class="n">G</span><span class="p">[</span><span class="mi">2</span><span class="p">,</span> <span class="mi">2</span><span class="p">],</span>
                <span class="mi">2</span> <span class="o">*</span> <span class="n">G</span><span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">],</span>
                <span class="mi">2</span> <span class="o">*</span> <span class="n">G</span><span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">2</span><span class="p">],</span>
                <span class="mi">2</span> <span class="o">*</span> <span class="n">G</span><span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">],</span>
            <span class="p">)</span>

            <span class="k">if</span> <span class="n">A</span> <span class="o">&gt;</span> <span class="n">B</span> <span class="o">+</span> <span class="n">e</span> <span class="ow">or</span> <span class="p">(</span><span class="nb">abs</span><span class="p">(</span><span class="n">A</span> <span class="o">-</span> <span class="n">B</span><span class="p">)</span> <span class="o">&lt;</span> <span class="n">e</span> <span class="ow">and</span> <span class="nb">abs</span><span class="p">(</span><span class="n">E</span><span class="p">)</span> <span class="o">&gt;</span> <span class="nb">abs</span><span class="p">(</span><span class="n">N</span><span class="p">)</span> <span class="o">+</span> <span class="n">e</span><span class="p">):</span>
                <span class="c1"># A1</span>
                <span class="n">M</span> <span class="o">=</span> <span class="p">[[</span><span class="mi">0</span><span class="p">,</span> <span class="o">-</span><span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">],</span> <span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">],</span> <span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="o">-</span><span class="mi">1</span><span class="p">]]</span>
                <span class="n">G</span> <span class="o">=</span> <span class="n">dot</span><span class="p">(</span><span class="n">transpose</span><span class="p">(</span><span class="n">M</span><span class="p">),</span> <span class="n">dot</span><span class="p">(</span><span class="n">G</span><span class="p">,</span> <span class="n">M</span><span class="p">))</span>
            <span class="k">if</span> <span class="p">(</span><span class="n">B</span> <span class="o">&gt;</span> <span class="n">C</span> <span class="o">+</span> <span class="n">e</span><span class="p">)</span> <span class="ow">or</span> <span class="p">(</span><span class="nb">abs</span><span class="p">(</span><span class="n">B</span> <span class="o">-</span> <span class="n">C</span><span class="p">)</span> <span class="o">&lt;</span> <span class="n">e</span> <span class="ow">and</span> <span class="nb">abs</span><span class="p">(</span><span class="n">N</span><span class="p">)</span> <span class="o">&gt;</span> <span class="nb">abs</span><span class="p">(</span><span class="n">Y</span><span class="p">)</span> <span class="o">+</span> <span class="n">e</span><span class="p">):</span>
                <span class="c1"># A2</span>
                <span class="n">M</span> <span class="o">=</span> <span class="p">[[</span><span class="o">-</span><span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">],</span> <span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="o">-</span><span class="mi">1</span><span class="p">],</span> <span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="o">-</span><span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">]]</span>
                <span class="n">G</span> <span class="o">=</span> <span class="n">dot</span><span class="p">(</span><span class="n">transpose</span><span class="p">(</span><span class="n">M</span><span class="p">),</span> <span class="n">dot</span><span class="p">(</span><span class="n">G</span><span class="p">,</span> <span class="n">M</span><span class="p">))</span>
                <span class="k">continue</span>

            <span class="n">l</span> <span class="o">=</span> <span class="mi">0</span> <span class="k">if</span> <span class="nb">abs</span><span class="p">(</span><span class="n">E</span><span class="p">)</span> <span class="o">&lt;</span> <span class="n">e</span> <span class="k">else</span> <span class="n">E</span> <span class="o">/</span> <span class="nb">abs</span><span class="p">(</span><span class="n">E</span><span class="p">)</span>
            <span class="n">m</span> <span class="o">=</span> <span class="mi">0</span> <span class="k">if</span> <span class="nb">abs</span><span class="p">(</span><span class="n">N</span><span class="p">)</span> <span class="o">&lt;</span> <span class="n">e</span> <span class="k">else</span> <span class="n">N</span> <span class="o">/</span> <span class="nb">abs</span><span class="p">(</span><span class="n">N</span><span class="p">)</span>
            <span class="n">n</span> <span class="o">=</span> <span class="mi">0</span> <span class="k">if</span> <span class="nb">abs</span><span class="p">(</span><span class="n">Y</span><span class="p">)</span> <span class="o">&lt;</span> <span class="n">e</span> <span class="k">else</span> <span class="n">Y</span> <span class="o">/</span> <span class="nb">abs</span><span class="p">(</span><span class="n">Y</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">l</span> <span class="o">*</span> <span class="n">m</span> <span class="o">*</span> <span class="n">n</span> <span class="o">==</span> <span class="mi">1</span><span class="p">:</span>
                <span class="c1"># A3</span>
                <span class="n">i</span> <span class="o">=</span> <span class="o">-</span><span class="mi">1</span> <span class="k">if</span> <span class="n">l</span> <span class="o">==</span> <span class="o">-</span><span class="mi">1</span> <span class="k">else</span> <span class="mi">1</span>
                <span class="n">j</span> <span class="o">=</span> <span class="o">-</span><span class="mi">1</span> <span class="k">if</span> <span class="n">m</span> <span class="o">==</span> <span class="o">-</span><span class="mi">1</span> <span class="k">else</span> <span class="mi">1</span>
                <span class="n">k</span> <span class="o">=</span> <span class="o">-</span><span class="mi">1</span> <span class="k">if</span> <span class="n">n</span> <span class="o">==</span> <span class="o">-</span><span class="mi">1</span> <span class="k">else</span> <span class="mi">1</span>
                <span class="n">M</span> <span class="o">=</span> <span class="p">[[</span><span class="n">i</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">],</span> <span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="n">j</span><span class="p">,</span> <span class="mi">0</span><span class="p">],</span> <span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="n">k</span><span class="p">]]</span>
                <span class="n">G</span> <span class="o">=</span> <span class="n">dot</span><span class="p">(</span><span class="n">transpose</span><span class="p">(</span><span class="n">M</span><span class="p">),</span> <span class="n">dot</span><span class="p">(</span><span class="n">G</span><span class="p">,</span> <span class="n">M</span><span class="p">))</span>
            <span class="k">elif</span> <span class="n">l</span> <span class="o">*</span> <span class="n">m</span> <span class="o">*</span> <span class="n">n</span> <span class="o">==</span> <span class="mi">0</span> <span class="ow">or</span> <span class="n">l</span> <span class="o">*</span> <span class="n">m</span> <span class="o">*</span> <span class="n">n</span> <span class="o">==</span> <span class="o">-</span><span class="mi">1</span><span class="p">:</span>
                <span class="c1"># A4</span>
                <span class="n">i</span> <span class="o">=</span> <span class="o">-</span><span class="mi">1</span> <span class="k">if</span> <span class="n">l</span> <span class="o">==</span> <span class="mi">1</span> <span class="k">else</span> <span class="mi">1</span>
                <span class="n">j</span> <span class="o">=</span> <span class="o">-</span><span class="mi">1</span> <span class="k">if</span> <span class="n">m</span> <span class="o">==</span> <span class="mi">1</span> <span class="k">else</span> <span class="mi">1</span>
                <span class="n">k</span> <span class="o">=</span> <span class="o">-</span><span class="mi">1</span> <span class="k">if</span> <span class="n">n</span> <span class="o">==</span> <span class="mi">1</span> <span class="k">else</span> <span class="mi">1</span>

                <span class="k">if</span> <span class="n">i</span> <span class="o">*</span> <span class="n">j</span> <span class="o">*</span> <span class="n">k</span> <span class="o">==</span> <span class="o">-</span><span class="mi">1</span><span class="p">:</span>
                    <span class="k">if</span> <span class="n">n</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
                        <span class="n">k</span> <span class="o">=</span> <span class="o">-</span><span class="mi">1</span>
                    <span class="k">elif</span> <span class="n">m</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
                        <span class="n">j</span> <span class="o">=</span> <span class="o">-</span><span class="mi">1</span>
                    <span class="k">elif</span> <span class="n">l</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
                        <span class="n">i</span> <span class="o">=</span> <span class="o">-</span><span class="mi">1</span>
                <span class="n">M</span> <span class="o">=</span> <span class="p">[[</span><span class="n">i</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">],</span> <span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="n">j</span><span class="p">,</span> <span class="mi">0</span><span class="p">],</span> <span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="n">k</span><span class="p">]]</span>
                <span class="n">G</span> <span class="o">=</span> <span class="n">dot</span><span class="p">(</span><span class="n">transpose</span><span class="p">(</span><span class="n">M</span><span class="p">),</span> <span class="n">dot</span><span class="p">(</span><span class="n">G</span><span class="p">,</span> <span class="n">M</span><span class="p">))</span>

            <span class="p">(</span><span class="n">A</span><span class="p">,</span> <span class="n">B</span><span class="p">,</span> <span class="n">C</span><span class="p">,</span> <span class="n">E</span><span class="p">,</span> <span class="n">N</span><span class="p">,</span> <span class="n">Y</span><span class="p">)</span> <span class="o">=</span> <span class="p">(</span>
                <span class="n">G</span><span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">],</span>
                <span class="n">G</span><span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">],</span>
                <span class="n">G</span><span class="p">[</span><span class="mi">2</span><span class="p">,</span> <span class="mi">2</span><span class="p">],</span>
                <span class="mi">2</span> <span class="o">*</span> <span class="n">G</span><span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">],</span>
                <span class="mi">2</span> <span class="o">*</span> <span class="n">G</span><span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">2</span><span class="p">],</span>
                <span class="mi">2</span> <span class="o">*</span> <span class="n">G</span><span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">],</span>
            <span class="p">)</span>

            <span class="c1"># A5</span>
            <span class="k">if</span> <span class="p">(</span>
                    <span class="nb">abs</span><span class="p">(</span><span class="n">E</span><span class="p">)</span> <span class="o">&gt;</span> <span class="n">B</span> <span class="o">+</span> <span class="n">e</span>
                    <span class="ow">or</span> <span class="p">(</span><span class="nb">abs</span><span class="p">(</span><span class="n">E</span> <span class="o">-</span> <span class="n">B</span><span class="p">)</span> <span class="o">&lt;</span> <span class="n">e</span> <span class="ow">and</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">N</span> <span class="o">&lt;</span> <span class="n">Y</span> <span class="o">-</span> <span class="n">e</span><span class="p">)</span>
                    <span class="ow">or</span> <span class="p">(</span><span class="nb">abs</span><span class="p">(</span><span class="n">E</span> <span class="o">+</span> <span class="n">B</span><span class="p">)</span> <span class="o">&lt;</span> <span class="n">e</span> <span class="ow">and</span> <span class="n">Y</span> <span class="o">&lt;</span> <span class="o">-</span><span class="n">e</span><span class="p">)</span>
            <span class="p">):</span>
                <span class="n">M</span> <span class="o">=</span> <span class="p">[[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">],</span> <span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="o">-</span><span class="n">E</span> <span class="o">/</span> <span class="nb">abs</span><span class="p">(</span><span class="n">E</span><span class="p">)],</span> <span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">]]</span>
                <span class="n">G</span> <span class="o">=</span> <span class="n">dot</span><span class="p">(</span><span class="n">transpose</span><span class="p">(</span><span class="n">M</span><span class="p">),</span> <span class="n">dot</span><span class="p">(</span><span class="n">G</span><span class="p">,</span> <span class="n">M</span><span class="p">))</span>
                <span class="k">continue</span>

            <span class="c1"># A6</span>
            <span class="k">if</span> <span class="p">(</span>
                    <span class="nb">abs</span><span class="p">(</span><span class="n">N</span><span class="p">)</span> <span class="o">&gt;</span> <span class="n">A</span> <span class="o">+</span> <span class="n">e</span>
                    <span class="ow">or</span> <span class="p">(</span><span class="nb">abs</span><span class="p">(</span><span class="n">A</span> <span class="o">-</span> <span class="n">N</span><span class="p">)</span> <span class="o">&lt;</span> <span class="n">e</span> <span class="ow">and</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">E</span> <span class="o">&lt;</span> <span class="n">Y</span> <span class="o">-</span> <span class="n">e</span><span class="p">)</span>
                    <span class="ow">or</span> <span class="p">(</span><span class="nb">abs</span><span class="p">(</span><span class="n">A</span> <span class="o">+</span> <span class="n">N</span><span class="p">)</span> <span class="o">&lt;</span> <span class="n">e</span> <span class="ow">and</span> <span class="n">Y</span> <span class="o">&lt;</span> <span class="o">-</span><span class="n">e</span><span class="p">)</span>
            <span class="p">):</span>
                <span class="n">M</span> <span class="o">=</span> <span class="p">[[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="o">-</span><span class="n">N</span> <span class="o">/</span> <span class="nb">abs</span><span class="p">(</span><span class="n">N</span><span class="p">)],</span> <span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">],</span> <span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">]]</span>
                <span class="n">G</span> <span class="o">=</span> <span class="n">dot</span><span class="p">(</span><span class="n">transpose</span><span class="p">(</span><span class="n">M</span><span class="p">),</span> <span class="n">dot</span><span class="p">(</span><span class="n">G</span><span class="p">,</span> <span class="n">M</span><span class="p">))</span>
                <span class="k">continue</span>

            <span class="c1"># A7</span>
            <span class="k">if</span> <span class="p">(</span>
                    <span class="nb">abs</span><span class="p">(</span><span class="n">Y</span><span class="p">)</span> <span class="o">&gt;</span> <span class="n">A</span> <span class="o">+</span> <span class="n">e</span>
                    <span class="ow">or</span> <span class="p">(</span><span class="nb">abs</span><span class="p">(</span><span class="n">A</span> <span class="o">-</span> <span class="n">Y</span><span class="p">)</span> <span class="o">&lt;</span> <span class="n">e</span> <span class="ow">and</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">E</span> <span class="o">&lt;</span> <span class="n">N</span> <span class="o">-</span> <span class="n">e</span><span class="p">)</span>
                    <span class="ow">or</span> <span class="p">(</span><span class="nb">abs</span><span class="p">(</span><span class="n">A</span> <span class="o">+</span> <span class="n">Y</span><span class="p">)</span> <span class="o">&lt;</span> <span class="n">e</span> <span class="ow">and</span> <span class="n">N</span> <span class="o">&lt;</span> <span class="o">-</span><span class="n">e</span><span class="p">)</span>
            <span class="p">):</span>
                <span class="n">M</span> <span class="o">=</span> <span class="p">[[</span><span class="mi">1</span><span class="p">,</span> <span class="o">-</span><span class="n">Y</span> <span class="o">/</span> <span class="nb">abs</span><span class="p">(</span><span class="n">Y</span><span class="p">),</span> <span class="mi">0</span><span class="p">],</span> <span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">],</span> <span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">]]</span>
                <span class="n">G</span> <span class="o">=</span> <span class="n">dot</span><span class="p">(</span><span class="n">transpose</span><span class="p">(</span><span class="n">M</span><span class="p">),</span> <span class="n">dot</span><span class="p">(</span><span class="n">G</span><span class="p">,</span> <span class="n">M</span><span class="p">))</span>
                <span class="k">continue</span>

            <span class="c1"># A8</span>
            <span class="k">if</span> <span class="n">E</span> <span class="o">+</span> <span class="n">N</span> <span class="o">+</span> <span class="n">Y</span> <span class="o">+</span> <span class="n">A</span> <span class="o">+</span> <span class="n">B</span> <span class="o">&lt;</span> <span class="o">-</span><span class="n">e</span> <span class="ow">or</span> <span class="p">(</span><span class="nb">abs</span><span class="p">(</span><span class="n">E</span> <span class="o">+</span> <span class="n">N</span> <span class="o">+</span> <span class="n">Y</span> <span class="o">+</span> <span class="n">A</span> <span class="o">+</span> <span class="n">B</span><span class="p">)</span> <span class="o">&lt;</span> <span class="n">e</span> <span class="o">&lt;</span> <span class="n">Y</span> <span class="o">+</span> <span class="p">(</span><span class="n">A</span> <span class="o">+</span> <span class="n">N</span><span class="p">)</span> <span class="o">*</span> <span class="mi">2</span><span class="p">):</span>
                <span class="n">M</span> <span class="o">=</span> <span class="p">[[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">],</span> <span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">],</span> <span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">]]</span>
                <span class="n">G</span> <span class="o">=</span> <span class="n">dot</span><span class="p">(</span><span class="n">transpose</span><span class="p">(</span><span class="n">M</span><span class="p">),</span> <span class="n">dot</span><span class="p">(</span><span class="n">G</span><span class="p">,</span> <span class="n">M</span><span class="p">))</span>
                <span class="k">continue</span>

            <span class="k">break</span>

        <span class="n">A</span> <span class="o">=</span> <span class="n">G</span><span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">]</span>
        <span class="n">B</span> <span class="o">=</span> <span class="n">G</span><span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">]</span>
        <span class="n">C</span> <span class="o">=</span> <span class="n">G</span><span class="p">[</span><span class="mi">2</span><span class="p">,</span> <span class="mi">2</span><span class="p">]</span>
        <span class="n">E</span> <span class="o">=</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">G</span><span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">]</span>
        <span class="n">N</span> <span class="o">=</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">G</span><span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">2</span><span class="p">]</span>
        <span class="n">Y</span> <span class="o">=</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">G</span><span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">]</span>
        <span class="n">a</span> <span class="o">=</span> <span class="n">math</span><span class="o">.</span><span class="n">sqrt</span><span class="p">(</span><span class="n">A</span><span class="p">)</span>
        <span class="n">b</span> <span class="o">=</span> <span class="n">math</span><span class="o">.</span><span class="n">sqrt</span><span class="p">(</span><span class="n">B</span><span class="p">)</span>
        <span class="n">c</span> <span class="o">=</span> <span class="n">math</span><span class="o">.</span><span class="n">sqrt</span><span class="p">(</span><span class="n">C</span><span class="p">)</span>
        <span class="n">alpha</span> <span class="o">=</span> <span class="n">math</span><span class="o">.</span><span class="n">acos</span><span class="p">(</span><span class="n">E</span> <span class="o">/</span> <span class="mi">2</span> <span class="o">/</span> <span class="n">b</span> <span class="o">/</span> <span class="n">c</span><span class="p">)</span> <span class="o">/</span> <span class="n">math</span><span class="o">.</span><span class="n">pi</span> <span class="o">*</span> <span class="mi">180</span>
        <span class="n">beta</span> <span class="o">=</span> <span class="n">math</span><span class="o">.</span><span class="n">acos</span><span class="p">(</span><span class="n">N</span> <span class="o">/</span> <span class="mi">2</span> <span class="o">/</span> <span class="n">a</span> <span class="o">/</span> <span class="n">c</span><span class="p">)</span> <span class="o">/</span> <span class="n">math</span><span class="o">.</span><span class="n">pi</span> <span class="o">*</span> <span class="mi">180</span>
        <span class="n">gamma</span> <span class="o">=</span> <span class="n">math</span><span class="o">.</span><span class="n">acos</span><span class="p">(</span><span class="n">Y</span> <span class="o">/</span> <span class="mi">2</span> <span class="o">/</span> <span class="n">a</span> <span class="o">/</span> <span class="n">b</span><span class="p">)</span> <span class="o">/</span> <span class="n">math</span><span class="o">.</span><span class="n">pi</span> <span class="o">*</span> <span class="mi">180</span>

        <span class="n">latt</span> <span class="o">=</span> <span class="n">Lattice</span><span class="o">.</span><span class="n">from_parameters</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">,</span> <span class="n">c</span><span class="p">,</span> <span class="n">alpha</span><span class="p">,</span> <span class="n">beta</span><span class="p">,</span> <span class="n">gamma</span><span class="p">)</span>

        <span class="n">mapped</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">find_mapping</span><span class="p">(</span><span class="n">latt</span><span class="p">,</span> <span class="n">e</span><span class="p">,</span> <span class="n">skip_rotation_matrix</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">mapped</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">np</span><span class="o">.</span><span class="n">linalg</span><span class="o">.</span><span class="n">det</span><span class="p">(</span><span class="n">mapped</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">matrix</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">:</span>
                <span class="k">return</span> <span class="n">mapped</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
            <span class="k">return</span> <span class="n">Lattice</span><span class="p">(</span><span class="o">-</span><span class="n">mapped</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">matrix</span><span class="p">)</span>

        <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;can&#39;t find niggli&quot;</span><span class="p">)</span></div>

<div class="viewcode-block" id="Lattice.scale"><a class="viewcode-back" href="../../../pymatgen.core.lattice.html#pymatgen.core.lattice.Lattice.scale">[docs]</a>    <span class="k">def</span> <span class="nf">scale</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">new_volume</span><span class="p">:</span> <span class="nb">float</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="s2">&quot;Lattice&quot;</span><span class="p">:</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Return a new Lattice with volume new_volume by performing a</span>
<span class="sd">        scaling of the lattice vectors so that length proportions and angles</span>
<span class="sd">        are preserved.</span>

<span class="sd">        Args:</span>
<span class="sd">            new_volume:</span>
<span class="sd">                New volume to scale to.</span>

<span class="sd">        Returns:</span>
<span class="sd">            New lattice with desired volume.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">versors</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">matrix</span> <span class="o">/</span> <span class="bp">self</span><span class="o">.</span><span class="n">abc</span>

        <span class="n">geo_factor</span> <span class="o">=</span> <span class="nb">abs</span><span class="p">(</span><span class="n">dot</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">cross</span><span class="p">(</span><span class="n">versors</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">versors</span><span class="p">[</span><span class="mi">1</span><span class="p">]),</span> <span class="n">versors</span><span class="p">[</span><span class="mi">2</span><span class="p">]))</span>

        <span class="n">ratios</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">abc</span><span class="p">)</span> <span class="o">/</span> <span class="bp">self</span><span class="o">.</span><span class="n">c</span>

        <span class="n">new_c</span> <span class="o">=</span> <span class="p">(</span><span class="n">new_volume</span> <span class="o">/</span> <span class="p">(</span><span class="n">geo_factor</span> <span class="o">*</span> <span class="n">np</span><span class="o">.</span><span class="n">prod</span><span class="p">(</span><span class="n">ratios</span><span class="p">)))</span> <span class="o">**</span> <span class="p">(</span><span class="mi">1</span> <span class="o">/</span> <span class="mf">3.0</span><span class="p">)</span>

        <span class="k">return</span> <span class="n">Lattice</span><span class="p">(</span><span class="n">versors</span> <span class="o">*</span> <span class="p">(</span><span class="n">new_c</span> <span class="o">*</span> <span class="n">ratios</span><span class="p">))</span></div>

<div class="viewcode-block" id="Lattice.get_wigner_seitz_cell"><a class="viewcode-back" href="../../../pymatgen.core.lattice.html#pymatgen.core.lattice.Lattice.get_wigner_seitz_cell">[docs]</a>    <span class="k">def</span> <span class="nf">get_wigner_seitz_cell</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">List</span><span class="p">[</span><span class="n">List</span><span class="p">[</span><span class="n">np</span><span class="o">.</span><span class="n">ndarray</span><span class="p">]]:</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Returns the Wigner-Seitz cell for the given lattice.</span>

<span class="sd">        Returns:</span>
<span class="sd">            A list of list of coordinates.</span>
<span class="sd">            Each element in the list is a &quot;facet&quot; of the boundary of the</span>
<span class="sd">            Wigner Seitz cell. For instance, a list of four coordinates will</span>
<span class="sd">            represent a square facet.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">vec1</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_matrix</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
        <span class="n">vec2</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_matrix</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span>
        <span class="n">vec3</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_matrix</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span>

        <span class="n">list_k_points</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">for</span> <span class="n">i</span><span class="p">,</span> <span class="n">j</span><span class="p">,</span> <span class="n">k</span> <span class="ow">in</span> <span class="n">itertools</span><span class="o">.</span><span class="n">product</span><span class="p">([</span><span class="o">-</span><span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">],</span> <span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">],</span> <span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">]):</span>
            <span class="n">list_k_points</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">i</span> <span class="o">*</span> <span class="n">vec1</span> <span class="o">+</span> <span class="n">j</span> <span class="o">*</span> <span class="n">vec2</span> <span class="o">+</span> <span class="n">k</span> <span class="o">*</span> <span class="n">vec3</span><span class="p">)</span>
        <span class="kn">from</span> <span class="nn">scipy.spatial</span> <span class="kn">import</span> <span class="n">Voronoi</span>

        <span class="n">tess</span> <span class="o">=</span> <span class="n">Voronoi</span><span class="p">(</span><span class="n">list_k_points</span><span class="p">)</span>
        <span class="n">to_return</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">for</span> <span class="n">r</span> <span class="ow">in</span> <span class="n">tess</span><span class="o">.</span><span class="n">ridge_dict</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">r</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">==</span> <span class="mi">13</span> <span class="ow">or</span> <span class="n">r</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">==</span> <span class="mi">13</span><span class="p">:</span>
                <span class="n">to_return</span><span class="o">.</span><span class="n">append</span><span class="p">([</span><span class="n">tess</span><span class="o">.</span><span class="n">vertices</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">tess</span><span class="o">.</span><span class="n">ridge_dict</span><span class="p">[</span><span class="n">r</span><span class="p">]])</span>

        <span class="k">return</span> <span class="n">to_return</span></div>

<div class="viewcode-block" id="Lattice.get_brillouin_zone"><a class="viewcode-back" href="../../../pymatgen.core.lattice.html#pymatgen.core.lattice.Lattice.get_brillouin_zone">[docs]</a>    <span class="k">def</span> <span class="nf">get_brillouin_zone</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">List</span><span class="p">[</span><span class="n">List</span><span class="p">[</span><span class="n">np</span><span class="o">.</span><span class="n">ndarray</span><span class="p">]]:</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Returns the Wigner-Seitz cell for the reciprocal lattice, aka the</span>
<span class="sd">        Brillouin Zone.</span>

<span class="sd">        Returns:</span>
<span class="sd">            A list of list of coordinates.</span>
<span class="sd">            Each element in the list is a &quot;facet&quot; of the boundary of the</span>
<span class="sd">            Brillouin Zone. For instance, a list of four coordinates will</span>
<span class="sd">            represent a square facet.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">reciprocal_lattice</span><span class="o">.</span><span class="n">get_wigner_seitz_cell</span><span class="p">()</span></div>

<div class="viewcode-block" id="Lattice.dot"><a class="viewcode-back" href="../../../pymatgen.core.lattice.html#pymatgen.core.lattice.Lattice.dot">[docs]</a>    <span class="k">def</span> <span class="nf">dot</span><span class="p">(</span>
            <span class="bp">self</span><span class="p">,</span> <span class="n">coords_a</span><span class="p">:</span> <span class="n">Vector3Like</span><span class="p">,</span> <span class="n">coords_b</span><span class="p">:</span> <span class="n">Vector3Like</span><span class="p">,</span> <span class="n">frac_coords</span><span class="p">:</span> <span class="nb">bool</span> <span class="o">=</span> <span class="kc">False</span>
    <span class="p">)</span> <span class="o">-&gt;</span> <span class="n">np</span><span class="o">.</span><span class="n">ndarray</span><span class="p">:</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Compute the scalar product of vector(s).</span>

<span class="sd">        Args:</span>
<span class="sd">            coords_a, coords_b: Array-like objects with the coordinates.</span>
<span class="sd">            frac_coords (bool): Boolean stating whether the vector</span>
<span class="sd">                corresponds to fractional or cartesian coordinates.</span>

<span class="sd">        Returns:</span>
<span class="sd">            one-dimensional `numpy` array.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">coords_a</span><span class="p">,</span> <span class="n">coords_b</span> <span class="o">=</span> <span class="p">(</span>
            <span class="n">np</span><span class="o">.</span><span class="n">reshape</span><span class="p">(</span><span class="n">coords_a</span><span class="p">,</span> <span class="p">(</span><span class="o">-</span><span class="mi">1</span><span class="p">,</span> <span class="mi">3</span><span class="p">)),</span>
            <span class="n">np</span><span class="o">.</span><span class="n">reshape</span><span class="p">(</span><span class="n">coords_b</span><span class="p">,</span> <span class="p">(</span><span class="o">-</span><span class="mi">1</span><span class="p">,</span> <span class="mi">3</span><span class="p">)),</span>
        <span class="p">)</span>

        <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">coords_a</span><span class="p">)</span> <span class="o">!=</span> <span class="nb">len</span><span class="p">(</span><span class="n">coords_b</span><span class="p">):</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;&quot;</span><span class="p">)</span>

        <span class="k">if</span> <span class="n">np</span><span class="o">.</span><span class="n">iscomplexobj</span><span class="p">(</span><span class="n">coords_a</span><span class="p">)</span> <span class="ow">or</span> <span class="n">np</span><span class="o">.</span><span class="n">iscomplexobj</span><span class="p">(</span><span class="n">coords_b</span><span class="p">):</span>
            <span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span><span class="s2">&quot;Complex array!&quot;</span><span class="p">)</span>

        <span class="k">if</span> <span class="ow">not</span> <span class="n">frac_coords</span><span class="p">:</span>
            <span class="n">cart_a</span><span class="p">,</span> <span class="n">cart_b</span> <span class="o">=</span> <span class="n">coords_a</span><span class="p">,</span> <span class="n">coords_b</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">cart_a</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">reshape</span><span class="p">(</span>
                <span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">get_cartesian_coords</span><span class="p">(</span><span class="n">vec</span><span class="p">)</span> <span class="k">for</span> <span class="n">vec</span> <span class="ow">in</span> <span class="n">coords_a</span><span class="p">],</span> <span class="p">(</span><span class="o">-</span><span class="mi">1</span><span class="p">,</span> <span class="mi">3</span><span class="p">)</span>
            <span class="p">)</span>
            <span class="n">cart_b</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">reshape</span><span class="p">(</span>
                <span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">get_cartesian_coords</span><span class="p">(</span><span class="n">vec</span><span class="p">)</span> <span class="k">for</span> <span class="n">vec</span> <span class="ow">in</span> <span class="n">coords_b</span><span class="p">],</span> <span class="p">(</span><span class="o">-</span><span class="mi">1</span><span class="p">,</span> <span class="mi">3</span><span class="p">)</span>
            <span class="p">)</span>

        <span class="k">return</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="n">dot</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">)</span> <span class="k">for</span> <span class="n">a</span><span class="p">,</span> <span class="n">b</span> <span class="ow">in</span> <span class="nb">zip</span><span class="p">(</span><span class="n">cart_a</span><span class="p">,</span> <span class="n">cart_b</span><span class="p">)])</span></div>

<div class="viewcode-block" id="Lattice.norm"><a class="viewcode-back" href="../../../pymatgen.core.lattice.html#pymatgen.core.lattice.Lattice.norm">[docs]</a>    <span class="k">def</span> <span class="nf">norm</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">coords</span><span class="p">:</span> <span class="n">Vector3Like</span><span class="p">,</span> <span class="n">frac_coords</span><span class="p">:</span> <span class="nb">bool</span> <span class="o">=</span> <span class="kc">True</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="nb">float</span><span class="p">:</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Compute the norm of vector(s).</span>

<span class="sd">        Args:</span>
<span class="sd">            coords:</span>
<span class="sd">                Array-like object with the coordinates.</span>
<span class="sd">            frac_coords:</span>
<span class="sd">                Boolean stating whether the vector corresponds to fractional or</span>
<span class="sd">                cartesian coordinates.</span>

<span class="sd">        Returns:</span>
<span class="sd">            one-dimensional `numpy` array.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="n">np</span><span class="o">.</span><span class="n">sqrt</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">dot</span><span class="p">(</span><span class="n">coords</span><span class="p">,</span> <span class="n">coords</span><span class="p">,</span> <span class="n">frac_coords</span><span class="o">=</span><span class="n">frac_coords</span><span class="p">))</span></div>

<div class="viewcode-block" id="Lattice.get_points_in_sphere"><a class="viewcode-back" href="../../../pymatgen.core.lattice.html#pymatgen.core.lattice.Lattice.get_points_in_sphere">[docs]</a>    <span class="k">def</span> <span class="nf">get_points_in_sphere</span><span class="p">(</span>
            <span class="bp">self</span><span class="p">,</span>
            <span class="n">frac_points</span><span class="p">:</span> <span class="n">List</span><span class="p">[</span><span class="n">Vector3Like</span><span class="p">],</span>
            <span class="n">center</span><span class="p">:</span> <span class="n">Vector3Like</span><span class="p">,</span>
            <span class="n">r</span><span class="p">:</span> <span class="nb">float</span><span class="p">,</span>
            <span class="n">zip_results</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span>
    <span class="p">)</span> <span class="o">-&gt;</span> <span class="n">Union</span><span class="p">[</span>
        <span class="n">List</span><span class="p">[</span><span class="n">Tuple</span><span class="p">[</span><span class="n">np</span><span class="o">.</span><span class="n">ndarray</span><span class="p">,</span> <span class="nb">float</span><span class="p">,</span> <span class="nb">int</span><span class="p">,</span> <span class="n">np</span><span class="o">.</span><span class="n">ndarray</span><span class="p">]],</span>
        <span class="n">List</span><span class="p">[</span><span class="n">np</span><span class="o">.</span><span class="n">ndarray</span><span class="p">],</span>
    <span class="p">]:</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Find all points within a sphere from the point taking into account</span>
<span class="sd">        periodic boundary conditions. This includes sites in other periodic</span>
<span class="sd">        images.</span>

<span class="sd">        Algorithm:</span>

<span class="sd">        1. place sphere of radius r in crystal and determine minimum supercell</span>
<span class="sd">           (parallelpiped) which would contain a sphere of radius r. for this</span>
<span class="sd">           we need the projection of a_1 on a unit vector perpendicular</span>
<span class="sd">           to a_2 &amp; a_3 (i.e. the unit vector in the direction b_1) to</span>
<span class="sd">           determine how many a_1&quot;s it will take to contain the sphere.</span>

<span class="sd">           Nxmax = r * length_of_b_1 / (2 Pi)</span>

<span class="sd">        2. keep points falling within r.</span>

<span class="sd">        Args:</span>
<span class="sd">            frac_points: All points in the lattice in fractional coordinates.</span>
<span class="sd">            center: Cartesian coordinates of center of sphere.</span>
<span class="sd">            r: radius of sphere.</span>
<span class="sd">            zip_results (bool): Whether to zip the results together to group by</span>
<span class="sd">                 point, or return the raw fcoord, dist, index arrays</span>

<span class="sd">        Returns:</span>
<span class="sd">            if zip_results:</span>
<span class="sd">                [(fcoord, dist, index, supercell_image) ...] since most of the time, subsequent</span>
<span class="sd">                processing requires the distance, index number of the atom, or index of the image</span>
<span class="sd">            else:</span>
<span class="sd">                fcoords, dists, inds, image</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="kn">from</span> <span class="nn">pymatgen.optimization.neighbors</span> <span class="kn">import</span> <span class="n">find_points_in_spheres</span>  <span class="c1"># type: ignore</span>
        <span class="k">except</span> <span class="ne">ImportError</span><span class="p">:</span>
            <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_points_in_sphere_py</span><span class="p">(</span><span class="n">frac_points</span><span class="o">=</span><span class="n">frac_points</span><span class="p">,</span> <span class="n">center</span><span class="o">=</span><span class="n">center</span><span class="p">,</span> <span class="n">r</span><span class="o">=</span><span class="n">r</span><span class="p">,</span> <span class="n">zip_results</span><span class="o">=</span><span class="n">zip_results</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">frac_points</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">ascontiguousarray</span><span class="p">(</span><span class="n">frac_points</span><span class="p">,</span> <span class="n">dtype</span><span class="o">=</span><span class="nb">float</span><span class="p">)</span>
            <span class="n">r</span> <span class="o">=</span> <span class="nb">float</span><span class="p">(</span><span class="n">r</span><span class="p">)</span>
            <span class="n">lattice_matrix</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">matrix</span><span class="p">)</span>
            <span class="n">lattice_matrix</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">ascontiguousarray</span><span class="p">(</span><span class="n">lattice_matrix</span><span class="p">)</span>
            <span class="n">cart_coords</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_cartesian_coords</span><span class="p">(</span><span class="n">frac_points</span><span class="p">)</span>
            <span class="n">_</span><span class="p">,</span> <span class="n">indices</span><span class="p">,</span> <span class="n">images</span><span class="p">,</span> <span class="n">distances</span> <span class="o">=</span> \
                <span class="n">find_points_in_spheres</span><span class="p">(</span><span class="n">all_coords</span><span class="o">=</span><span class="n">cart_coords</span><span class="p">,</span>
                                       <span class="n">center_coords</span><span class="o">=</span><span class="n">np</span><span class="o">.</span><span class="n">ascontiguousarray</span><span class="p">([</span><span class="n">center</span><span class="p">],</span> <span class="n">dtype</span><span class="o">=</span><span class="nb">float</span><span class="p">),</span>
                                       <span class="n">r</span><span class="o">=</span><span class="n">r</span><span class="p">,</span> <span class="n">pbc</span><span class="o">=</span><span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">]),</span> <span class="n">lattice</span><span class="o">=</span><span class="n">lattice_matrix</span><span class="p">,</span> <span class="n">tol</span><span class="o">=</span><span class="mf">1e-8</span><span class="p">)</span>
            <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">indices</span><span class="p">)</span> <span class="o">&lt;</span> <span class="mi">1</span><span class="p">:</span>
                <span class="k">return</span> <span class="p">[]</span> <span class="k">if</span> <span class="n">zip_results</span> <span class="k">else</span> <span class="p">[()]</span> <span class="o">*</span> <span class="mi">4</span>
            <span class="n">fcoords</span> <span class="o">=</span> <span class="n">frac_points</span><span class="p">[</span><span class="n">indices</span><span class="p">]</span> <span class="o">+</span> <span class="n">images</span>
            <span class="k">if</span> <span class="n">zip_results</span><span class="p">:</span>
                <span class="k">return</span> <span class="nb">list</span><span class="p">(</span>
                    <span class="nb">zip</span><span class="p">(</span>
                        <span class="n">fcoords</span><span class="p">,</span>
                        <span class="n">distances</span><span class="p">,</span>
                        <span class="n">indices</span><span class="p">,</span>
                        <span class="n">images</span><span class="p">,</span>
                    <span class="p">)</span>
                <span class="p">)</span>
            <span class="k">return</span> <span class="p">[</span>
                <span class="n">fcoords</span><span class="p">,</span>
                <span class="n">distances</span><span class="p">,</span>
                <span class="n">indices</span><span class="p">,</span>
                <span class="n">images</span><span class="p">,</span>
            <span class="p">]</span></div>

<div class="viewcode-block" id="Lattice.get_points_in_sphere_py"><a class="viewcode-back" href="../../../pymatgen.core.lattice.html#pymatgen.core.lattice.Lattice.get_points_in_sphere_py">[docs]</a>    <span class="k">def</span> <span class="nf">get_points_in_sphere_py</span><span class="p">(</span>
            <span class="bp">self</span><span class="p">,</span>
            <span class="n">frac_points</span><span class="p">:</span> <span class="n">List</span><span class="p">[</span><span class="n">Vector3Like</span><span class="p">],</span>
            <span class="n">center</span><span class="p">:</span> <span class="n">Vector3Like</span><span class="p">,</span>
            <span class="n">r</span><span class="p">:</span> <span class="nb">float</span><span class="p">,</span>
            <span class="n">zip_results</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span>
    <span class="p">)</span> <span class="o">-&gt;</span> <span class="n">Union</span><span class="p">[</span>
        <span class="n">List</span><span class="p">[</span><span class="n">Tuple</span><span class="p">[</span><span class="n">np</span><span class="o">.</span><span class="n">ndarray</span><span class="p">,</span> <span class="nb">float</span><span class="p">,</span> <span class="nb">int</span><span class="p">,</span> <span class="n">np</span><span class="o">.</span><span class="n">ndarray</span><span class="p">]],</span>
        <span class="n">List</span><span class="p">[</span><span class="n">np</span><span class="o">.</span><span class="n">ndarray</span><span class="p">],</span>
    <span class="p">]:</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Find all points within a sphere from the point taking into account</span>
<span class="sd">        periodic boundary conditions. This includes sites in other periodic</span>
<span class="sd">        images.</span>

<span class="sd">        Algorithm:</span>

<span class="sd">        1. place sphere of radius r in crystal and determine minimum supercell</span>
<span class="sd">           (parallelpiped) which would contain a sphere of radius r. for this</span>
<span class="sd">           we need the projection of a_1 on a unit vector perpendicular</span>
<span class="sd">           to a_2 &amp; a_3 (i.e. the unit vector in the direction b_1) to</span>
<span class="sd">           determine how many a_1&quot;s it will take to contain the sphere.</span>

<span class="sd">           Nxmax = r * length_of_b_1 / (2 Pi)</span>

<span class="sd">        2. keep points falling within r.</span>

<span class="sd">        Args:</span>
<span class="sd">            frac_points: All points in the lattice in fractional coordinates.</span>
<span class="sd">            center: Cartesian coordinates of center of sphere.</span>
<span class="sd">            r: radius of sphere.</span>
<span class="sd">            zip_results (bool): Whether to zip the results together to group by</span>
<span class="sd">                 point, or return the raw fcoord, dist, index arrays</span>

<span class="sd">        Returns:</span>
<span class="sd">            if zip_results:</span>
<span class="sd">                [(fcoord, dist, index, supercell_image) ...] since most of the time, subsequent</span>
<span class="sd">                processing requires the distance, index number of the atom, or index of the image</span>
<span class="sd">            else:</span>
<span class="sd">                fcoords, dists, inds, image</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">cart_coords</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_cartesian_coords</span><span class="p">(</span><span class="n">frac_points</span><span class="p">)</span>
        <span class="n">neighbors</span> <span class="o">=</span> <span class="n">get_points_in_spheres</span><span class="p">(</span><span class="n">all_coords</span><span class="o">=</span><span class="n">cart_coords</span><span class="p">,</span> <span class="n">center_coords</span><span class="o">=</span><span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="n">center</span><span class="p">]),</span> <span class="n">r</span><span class="o">=</span><span class="n">r</span><span class="p">,</span> <span class="n">pbc</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span>
                                          <span class="n">numerical_tol</span><span class="o">=</span><span class="mf">1e-8</span><span class="p">,</span> <span class="n">lattice</span><span class="o">=</span><span class="bp">self</span><span class="p">,</span> <span class="n">return_fcoords</span><span class="o">=</span><span class="kc">True</span><span class="p">)[</span><span class="mi">0</span><span class="p">]</span>
        <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">neighbors</span><span class="p">)</span> <span class="o">&lt;</span> <span class="mi">1</span><span class="p">:</span>
            <span class="k">return</span> <span class="p">[]</span> <span class="k">if</span> <span class="n">zip_results</span> <span class="k">else</span> <span class="p">[()]</span> <span class="o">*</span> <span class="mi">4</span>
        <span class="k">if</span> <span class="n">zip_results</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">neighbors</span>
        <span class="k">return</span> <span class="p">[</span><span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">i</span><span class="p">)</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">list</span><span class="p">(</span><span class="nb">zip</span><span class="p">(</span><span class="o">*</span><span class="n">neighbors</span><span class="p">))]</span></div>

    <span class="nd">@deprecated</span><span class="p">(</span><span class="n">get_points_in_sphere</span><span class="p">,</span> <span class="s2">&quot;This is retained purely for checking purposes.&quot;</span><span class="p">)</span>
    <span class="k">def</span> <span class="nf">get_points_in_sphere_old</span><span class="p">(</span>
            <span class="bp">self</span><span class="p">,</span>
            <span class="n">frac_points</span><span class="p">:</span> <span class="n">List</span><span class="p">[</span><span class="n">Vector3Like</span><span class="p">],</span>
            <span class="n">center</span><span class="p">:</span> <span class="n">Vector3Like</span><span class="p">,</span>
            <span class="n">r</span><span class="p">:</span> <span class="nb">float</span><span class="p">,</span>
            <span class="n">zip_results</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span>
    <span class="p">)</span> <span class="o">-&gt;</span> <span class="n">Union</span><span class="p">[</span>
        <span class="n">List</span><span class="p">[</span><span class="n">Tuple</span><span class="p">[</span><span class="n">np</span><span class="o">.</span><span class="n">ndarray</span><span class="p">,</span> <span class="nb">float</span><span class="p">,</span> <span class="nb">int</span><span class="p">,</span> <span class="n">np</span><span class="o">.</span><span class="n">ndarray</span><span class="p">]],</span>
        <span class="n">Tuple</span><span class="p">[</span><span class="n">List</span><span class="p">[</span><span class="n">np</span><span class="o">.</span><span class="n">ndarray</span><span class="p">],</span> <span class="n">List</span><span class="p">[</span><span class="nb">float</span><span class="p">],</span> <span class="n">List</span><span class="p">[</span><span class="nb">int</span><span class="p">],</span> <span class="n">List</span><span class="p">[</span><span class="n">np</span><span class="o">.</span><span class="n">ndarray</span><span class="p">]],</span>
    <span class="p">]:</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Find all points within a sphere from the point taking into account</span>
<span class="sd">        periodic boundary conditions. This includes sites in other periodic</span>
<span class="sd">        images.</span>

<span class="sd">        Algorithm:</span>

<span class="sd">        1. place sphere of radius r in crystal and determine minimum supercell</span>
<span class="sd">           (parallelpiped) which would contain a sphere of radius r. for this</span>
<span class="sd">           we need the projection of a_1 on a unit vector perpendicular</span>
<span class="sd">           to a_2 &amp; a_3 (i.e. the unit vector in the direction b_1) to</span>
<span class="sd">           determine how many a_1&quot;s it will take to contain the sphere.</span>

<span class="sd">           Nxmax = r * length_of_b_1 / (2 Pi)</span>

<span class="sd">        2. keep points falling within r.</span>

<span class="sd">        Args:</span>
<span class="sd">            frac_points: All points in the lattice in fractional coordinates.</span>
<span class="sd">            center: Cartesian coordinates of center of sphere.</span>
<span class="sd">            r: radius of sphere.</span>
<span class="sd">            zip_results (bool): Whether to zip the results together to group by</span>
<span class="sd">                 point, or return the raw fcoord, dist, index arrays</span>

<span class="sd">        Returns:</span>
<span class="sd">            if zip_results:</span>
<span class="sd">                [(fcoord, dist, index, supercell_image) ...] since most of the time, subsequent</span>
<span class="sd">                processing requires the distance, index number of the atom, or index of the image</span>
<span class="sd">            else:</span>
<span class="sd">                fcoords, dists, inds, image</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="c1"># TODO: refactor to use lll matrix (nmax will be smaller)</span>
        <span class="c1"># Determine the maximum number of supercells in each direction</span>
        <span class="c1">#  required to contain a sphere of radius n</span>
        <span class="n">recp_len</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">reciprocal_lattice</span><span class="o">.</span><span class="n">abc</span><span class="p">)</span> <span class="o">/</span> <span class="p">(</span><span class="mi">2</span> <span class="o">*</span> <span class="n">pi</span><span class="p">)</span>
        <span class="n">nmax</span> <span class="o">=</span> <span class="nb">float</span><span class="p">(</span><span class="n">r</span><span class="p">)</span> <span class="o">*</span> <span class="n">recp_len</span> <span class="o">+</span> <span class="mf">0.01</span>

        <span class="c1"># Get the fractional coordinates of the center of the sphere</span>
        <span class="n">pcoords</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_fractional_coords</span><span class="p">(</span><span class="n">center</span><span class="p">)</span>
        <span class="n">center</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">center</span><span class="p">)</span>

        <span class="c1"># Prepare the list of output atoms</span>
        <span class="n">n</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">frac_points</span><span class="p">)</span>
        <span class="n">fcoords</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">frac_points</span><span class="p">)</span> <span class="o">%</span> <span class="mi">1</span>
        <span class="n">indices</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="n">n</span><span class="p">)</span>

        <span class="c1"># Generate all possible images that could be within `r` of `center`</span>
        <span class="n">mins</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">floor</span><span class="p">(</span><span class="n">pcoords</span> <span class="o">-</span> <span class="n">nmax</span><span class="p">)</span>
        <span class="n">maxes</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">ceil</span><span class="p">(</span><span class="n">pcoords</span> <span class="o">+</span> <span class="n">nmax</span><span class="p">)</span>
        <span class="n">arange</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="n">start</span><span class="o">=</span><span class="n">mins</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">stop</span><span class="o">=</span><span class="n">maxes</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">dtype</span><span class="o">=</span><span class="n">np</span><span class="o">.</span><span class="n">int</span><span class="p">)</span>
        <span class="n">brange</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="n">start</span><span class="o">=</span><span class="n">mins</span><span class="p">[</span><span class="mi">1</span><span class="p">],</span> <span class="n">stop</span><span class="o">=</span><span class="n">maxes</span><span class="p">[</span><span class="mi">1</span><span class="p">],</span> <span class="n">dtype</span><span class="o">=</span><span class="n">np</span><span class="o">.</span><span class="n">int</span><span class="p">)</span>
        <span class="n">crange</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="n">start</span><span class="o">=</span><span class="n">mins</span><span class="p">[</span><span class="mi">2</span><span class="p">],</span> <span class="n">stop</span><span class="o">=</span><span class="n">maxes</span><span class="p">[</span><span class="mi">2</span><span class="p">],</span> <span class="n">dtype</span><span class="o">=</span><span class="n">np</span><span class="o">.</span><span class="n">int</span><span class="p">)</span>
        <span class="n">arange</span> <span class="o">=</span> <span class="n">arange</span><span class="p">[:,</span> <span class="kc">None</span><span class="p">]</span> <span class="o">*</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">],</span> <span class="n">dtype</span><span class="o">=</span><span class="n">np</span><span class="o">.</span><span class="n">int</span><span class="p">)[</span><span class="kc">None</span><span class="p">,</span> <span class="p">:]</span>
        <span class="n">brange</span> <span class="o">=</span> <span class="n">brange</span><span class="p">[:,</span> <span class="kc">None</span><span class="p">]</span> <span class="o">*</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">],</span> <span class="n">dtype</span><span class="o">=</span><span class="n">np</span><span class="o">.</span><span class="n">int</span><span class="p">)[</span><span class="kc">None</span><span class="p">,</span> <span class="p">:]</span>
        <span class="n">crange</span> <span class="o">=</span> <span class="n">crange</span><span class="p">[:,</span> <span class="kc">None</span><span class="p">]</span> <span class="o">*</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">],</span> <span class="n">dtype</span><span class="o">=</span><span class="n">np</span><span class="o">.</span><span class="n">int</span><span class="p">)[</span><span class="kc">None</span><span class="p">,</span> <span class="p">:]</span>
        <span class="n">images</span> <span class="o">=</span> <span class="n">arange</span><span class="p">[:,</span> <span class="kc">None</span><span class="p">,</span> <span class="kc">None</span><span class="p">]</span> <span class="o">+</span> <span class="n">brange</span><span class="p">[</span><span class="kc">None</span><span class="p">,</span> <span class="p">:,</span> <span class="kc">None</span><span class="p">]</span> <span class="o">+</span> <span class="n">crange</span><span class="p">[</span><span class="kc">None</span><span class="p">,</span> <span class="kc">None</span><span class="p">,</span> <span class="p">:]</span>

        <span class="c1"># Generate the coordinates of all atoms within these images</span>
        <span class="n">shifted_coords</span> <span class="o">=</span> <span class="n">fcoords</span><span class="p">[:,</span> <span class="kc">None</span><span class="p">,</span> <span class="kc">None</span><span class="p">,</span> <span class="kc">None</span><span class="p">,</span> <span class="p">:]</span> <span class="o">+</span> <span class="n">images</span><span class="p">[</span><span class="kc">None</span><span class="p">,</span> <span class="p">:,</span> <span class="p">:,</span> <span class="p">:,</span> <span class="p">:]</span>

        <span class="c1"># Determine distance from `center`</span>
        <span class="n">cart_coords</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_cartesian_coords</span><span class="p">(</span><span class="n">fcoords</span><span class="p">)</span>
        <span class="n">cart_images</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_cartesian_coords</span><span class="p">(</span><span class="n">images</span><span class="p">)</span>
        <span class="n">coords</span> <span class="o">=</span> <span class="n">cart_coords</span><span class="p">[:,</span> <span class="kc">None</span><span class="p">,</span> <span class="kc">None</span><span class="p">,</span> <span class="kc">None</span><span class="p">,</span> <span class="p">:]</span> <span class="o">+</span> <span class="n">cart_images</span><span class="p">[</span><span class="kc">None</span><span class="p">,</span> <span class="p">:,</span> <span class="p">:,</span> <span class="p">:,</span> <span class="p">:]</span>
        <span class="n">coords</span> <span class="o">-=</span> <span class="n">center</span><span class="p">[</span><span class="kc">None</span><span class="p">,</span> <span class="kc">None</span><span class="p">,</span> <span class="kc">None</span><span class="p">,</span> <span class="kc">None</span><span class="p">,</span> <span class="p">:]</span>
        <span class="n">coords</span> <span class="o">**=</span> <span class="mi">2</span>
        <span class="n">d_2</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">sum</span><span class="p">(</span><span class="n">coords</span><span class="p">,</span> <span class="n">axis</span><span class="o">=</span><span class="mi">4</span><span class="p">)</span>

        <span class="c1"># Determine which points are within `r` of `center`</span>
        <span class="n">within_r</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">where</span><span class="p">(</span><span class="n">d_2</span> <span class="o">&lt;=</span> <span class="n">r</span> <span class="o">**</span> <span class="mi">2</span><span class="p">)</span>
        <span class="c1">#  `within_r` now contains the coordinates of each image that is</span>
        <span class="c1">#    inside of the cutoff distance. It has 4 coordinates:</span>
        <span class="c1">#   0 - index of the image within `frac_points`</span>
        <span class="c1">#   1,2,3 - index of the supercell which holds the images in the x, y, z directions</span>

        <span class="k">if</span> <span class="n">zip_results</span><span class="p">:</span>
            <span class="k">return</span> <span class="nb">list</span><span class="p">(</span>
                <span class="nb">zip</span><span class="p">(</span>
                    <span class="n">shifted_coords</span><span class="p">[</span><span class="n">within_r</span><span class="p">],</span>
                    <span class="n">np</span><span class="o">.</span><span class="n">sqrt</span><span class="p">(</span><span class="n">d_2</span><span class="p">[</span><span class="n">within_r</span><span class="p">]),</span>
                    <span class="n">indices</span><span class="p">[</span><span class="n">within_r</span><span class="p">[</span><span class="mi">0</span><span class="p">]],</span>
                    <span class="n">images</span><span class="p">[</span><span class="n">within_r</span><span class="p">[</span><span class="mi">1</span><span class="p">:]],</span>
                <span class="p">)</span>
            <span class="p">)</span>
        <span class="k">return</span> <span class="p">(</span>
            <span class="n">shifted_coords</span><span class="p">[</span><span class="n">within_r</span><span class="p">],</span>
            <span class="n">np</span><span class="o">.</span><span class="n">sqrt</span><span class="p">(</span><span class="n">d_2</span><span class="p">[</span><span class="n">within_r</span><span class="p">]),</span>
            <span class="n">indices</span><span class="p">[</span><span class="n">within_r</span><span class="p">[</span><span class="mi">0</span><span class="p">]],</span>
            <span class="n">images</span><span class="p">[</span><span class="n">within_r</span><span class="p">[</span><span class="mi">1</span><span class="p">:]],</span>
        <span class="p">)</span>

<div class="viewcode-block" id="Lattice.get_all_distances"><a class="viewcode-back" href="../../../pymatgen.core.lattice.html#pymatgen.core.lattice.Lattice.get_all_distances">[docs]</a>    <span class="k">def</span> <span class="nf">get_all_distances</span><span class="p">(</span>
            <span class="bp">self</span><span class="p">,</span>
            <span class="n">fcoords1</span><span class="p">:</span> <span class="n">Union</span><span class="p">[</span><span class="n">Vector3Like</span><span class="p">,</span> <span class="n">List</span><span class="p">[</span><span class="n">Vector3Like</span><span class="p">]],</span>
            <span class="n">fcoords2</span><span class="p">:</span> <span class="n">Union</span><span class="p">[</span><span class="n">Vector3Like</span><span class="p">,</span> <span class="n">List</span><span class="p">[</span><span class="n">Vector3Like</span><span class="p">]],</span>
    <span class="p">)</span> <span class="o">-&gt;</span> <span class="n">np</span><span class="o">.</span><span class="n">ndarray</span><span class="p">:</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Returns the distances between two lists of coordinates taking into</span>
<span class="sd">        account periodic boundary conditions and the lattice. Note that this</span>
<span class="sd">        computes an MxN array of distances (i.e. the distance between each</span>
<span class="sd">        point in fcoords1 and every coordinate in fcoords2). This is</span>
<span class="sd">        different functionality from pbc_diff.</span>

<span class="sd">        Args:</span>
<span class="sd">            fcoords1: First set of fractional coordinates. e.g., [0.5, 0.6,</span>
<span class="sd">                0.7] or [[1.1, 1.2, 4.3], [0.5, 0.6, 0.7]]. It can be a single</span>
<span class="sd">                coord or any array of coords.</span>
<span class="sd">            fcoords2: Second set of fractional coordinates.</span>

<span class="sd">        Returns:</span>
<span class="sd">            2d array of cartesian distances. E.g the distance between</span>
<span class="sd">            fcoords1[i] and fcoords2[j] is distances[i,j]</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">v</span><span class="p">,</span> <span class="n">d2</span> <span class="o">=</span> <span class="n">pbc_shortest_vectors</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">fcoords1</span><span class="p">,</span> <span class="n">fcoords2</span><span class="p">,</span> <span class="n">return_d2</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">np</span><span class="o">.</span><span class="n">sqrt</span><span class="p">(</span><span class="n">d2</span><span class="p">)</span></div>

<div class="viewcode-block" id="Lattice.is_hexagonal"><a class="viewcode-back" href="../../../pymatgen.core.lattice.html#pymatgen.core.lattice.Lattice.is_hexagonal">[docs]</a>    <span class="k">def</span> <span class="nf">is_hexagonal</span><span class="p">(</span>
            <span class="bp">self</span><span class="p">,</span> <span class="n">hex_angle_tol</span><span class="p">:</span> <span class="nb">float</span> <span class="o">=</span> <span class="mi">5</span><span class="p">,</span> <span class="n">hex_length_tol</span><span class="p">:</span> <span class="nb">float</span> <span class="o">=</span> <span class="mf">0.01</span>
    <span class="p">)</span> <span class="o">-&gt;</span> <span class="nb">bool</span><span class="p">:</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        :param hex_angle_tol: Angle tolerance</span>
<span class="sd">        :param hex_length_tol: Length tolerance</span>
<span class="sd">        :return: Whether lattice corresponds to hexagonal lattice.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">lengths</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">lengths</span>
        <span class="n">angles</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">angles</span>
        <span class="n">right_angles</span> <span class="o">=</span> <span class="p">[</span><span class="n">i</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">3</span><span class="p">)</span> <span class="k">if</span> <span class="nb">abs</span><span class="p">(</span><span class="n">angles</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">-</span> <span class="mi">90</span><span class="p">)</span> <span class="o">&lt;</span> <span class="n">hex_angle_tol</span><span class="p">]</span>
        <span class="n">hex_angles</span> <span class="o">=</span> <span class="p">[</span><span class="n">i</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">3</span><span class="p">)</span>
                      <span class="k">if</span> <span class="nb">abs</span><span class="p">(</span><span class="n">angles</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">-</span> <span class="mi">60</span><span class="p">)</span> <span class="o">&lt;</span> <span class="n">hex_angle_tol</span> <span class="ow">or</span> <span class="nb">abs</span><span class="p">(</span><span class="n">angles</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">-</span> <span class="mi">120</span><span class="p">)</span> <span class="o">&lt;</span> <span class="n">hex_angle_tol</span><span class="p">]</span>

        <span class="k">return</span> <span class="p">(</span>
            <span class="nb">len</span><span class="p">(</span><span class="n">right_angles</span><span class="p">)</span> <span class="o">==</span> <span class="mi">2</span> <span class="ow">and</span>
            <span class="nb">len</span><span class="p">(</span><span class="n">hex_angles</span><span class="p">)</span> <span class="o">==</span> <span class="mi">1</span> <span class="ow">and</span>
            <span class="nb">abs</span><span class="p">(</span><span class="n">lengths</span><span class="p">[</span><span class="n">right_angles</span><span class="p">[</span><span class="mi">0</span><span class="p">]]</span> <span class="o">-</span> <span class="n">lengths</span><span class="p">[</span><span class="n">right_angles</span><span class="p">[</span><span class="mi">1</span><span class="p">]])</span> <span class="o">&lt;</span> <span class="n">hex_length_tol</span>
        <span class="p">)</span></div>

<div class="viewcode-block" id="Lattice.get_distance_and_image"><a class="viewcode-back" href="../../../pymatgen.core.lattice.html#pymatgen.core.lattice.Lattice.get_distance_and_image">[docs]</a>    <span class="k">def</span> <span class="nf">get_distance_and_image</span><span class="p">(</span>
            <span class="bp">self</span><span class="p">,</span>
            <span class="n">frac_coords1</span><span class="p">:</span> <span class="n">Vector3Like</span><span class="p">,</span>
            <span class="n">frac_coords2</span><span class="p">:</span> <span class="n">Vector3Like</span><span class="p">,</span>
            <span class="n">jimage</span><span class="p">:</span> <span class="n">Optional</span><span class="p">[</span><span class="n">Union</span><span class="p">[</span><span class="n">List</span><span class="p">[</span><span class="nb">int</span><span class="p">],</span> <span class="n">np</span><span class="o">.</span><span class="n">ndarray</span><span class="p">]]</span> <span class="o">=</span> <span class="kc">None</span><span class="p">,</span>
    <span class="p">)</span> <span class="o">-&gt;</span> <span class="n">Tuple</span><span class="p">[</span><span class="nb">float</span><span class="p">,</span> <span class="n">np</span><span class="o">.</span><span class="n">ndarray</span><span class="p">]:</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Gets distance between two frac_coords assuming periodic boundary</span>
<span class="sd">        conditions. If the index jimage is not specified it selects the j</span>
<span class="sd">        image nearest to the i atom and returns the distance and jimage</span>
<span class="sd">        indices in terms of lattice vector translations. If the index jimage</span>
<span class="sd">        is specified it returns the distance between the frac_coords1 and</span>
<span class="sd">        the specified jimage of frac_coords2, and the given jimage is also</span>
<span class="sd">        returned.</span>

<span class="sd">        Args:</span>
<span class="sd">            frac_coords1 (3x1 array): Reference fcoords to get distance from.</span>
<span class="sd">            frac)coords2 (3x1 array): fcoords to get distance from.</span>
<span class="sd">            jimage (3x1 array): Specific periodic image in terms of</span>
<span class="sd">                lattice translations, e.g., [1,0,0] implies to take periodic</span>
<span class="sd">                image that is one a-lattice vector away. If jimage is None,</span>
<span class="sd">                the image that is nearest to the site is found.</span>

<span class="sd">        Returns:</span>
<span class="sd">            (distance, jimage): distance and periodic lattice translations</span>
<span class="sd">            of the other site for which the distance applies. This means that</span>
<span class="sd">            the distance between frac_coords1 and (jimage + frac_coords2) is</span>
<span class="sd">            equal to distance.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="n">jimage</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">v</span><span class="p">,</span> <span class="n">d2</span> <span class="o">=</span> <span class="n">pbc_shortest_vectors</span><span class="p">(</span>
                <span class="bp">self</span><span class="p">,</span> <span class="n">frac_coords1</span><span class="p">,</span> <span class="n">frac_coords2</span><span class="p">,</span> <span class="n">return_d2</span><span class="o">=</span><span class="kc">True</span>
            <span class="p">)</span>
            <span class="n">fc</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_fractional_coords</span><span class="p">(</span><span class="n">v</span><span class="p">[</span><span class="mi">0</span><span class="p">][</span><span class="mi">0</span><span class="p">])</span> <span class="o">+</span> <span class="n">frac_coords1</span> <span class="o">-</span> <span class="n">frac_coords2</span>
            <span class="n">fc</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">round</span><span class="p">(</span><span class="n">fc</span><span class="p">),</span> <span class="n">dtype</span><span class="o">=</span><span class="n">np</span><span class="o">.</span><span class="n">int</span><span class="p">)</span>
            <span class="k">return</span> <span class="n">np</span><span class="o">.</span><span class="n">sqrt</span><span class="p">(</span><span class="n">d2</span><span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">]),</span> <span class="n">fc</span>

        <span class="n">jimage</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">jimage</span><span class="p">)</span>
        <span class="n">mapped_vec</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_cartesian_coords</span><span class="p">(</span><span class="n">jimage</span> <span class="o">+</span> <span class="n">frac_coords2</span> <span class="o">-</span> <span class="n">frac_coords1</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">np</span><span class="o">.</span><span class="n">linalg</span><span class="o">.</span><span class="n">norm</span><span class="p">(</span><span class="n">mapped_vec</span><span class="p">),</span> <span class="n">jimage</span></div>

<div class="viewcode-block" id="Lattice.get_miller_index_from_coords"><a class="viewcode-back" href="../../../pymatgen.core.lattice.html#pymatgen.core.lattice.Lattice.get_miller_index_from_coords">[docs]</a>    <span class="k">def</span> <span class="nf">get_miller_index_from_coords</span><span class="p">(</span>
            <span class="bp">self</span><span class="p">,</span>
            <span class="n">coords</span><span class="p">:</span> <span class="n">Vector3Like</span><span class="p">,</span>
            <span class="n">coords_are_cartesian</span><span class="p">:</span> <span class="nb">bool</span> <span class="o">=</span> <span class="kc">True</span><span class="p">,</span>
            <span class="n">round_dp</span><span class="p">:</span> <span class="nb">int</span> <span class="o">=</span> <span class="mi">4</span><span class="p">,</span>
            <span class="n">verbose</span><span class="p">:</span> <span class="nb">bool</span> <span class="o">=</span> <span class="kc">True</span><span class="p">,</span>
    <span class="p">)</span> <span class="o">-&gt;</span> <span class="n">Tuple</span><span class="p">[</span><span class="nb">int</span><span class="p">,</span> <span class="nb">int</span><span class="p">,</span> <span class="nb">int</span><span class="p">]:</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Get the Miller index of a plane from a list of site coordinates.</span>

<span class="sd">        A minimum of 3 sets of coordinates are required. If more than 3 sets of</span>
<span class="sd">        coordinates are given, the best plane that minimises the distance to all</span>
<span class="sd">        points will be calculated.</span>

<span class="sd">        Args:</span>
<span class="sd">            coords (iterable): A list or numpy array of coordinates. Can be</span>
<span class="sd">                cartesian or fractional coordinates. If more than three sets of</span>
<span class="sd">                coordinates are provided, the best plane that minimises the</span>
<span class="sd">                distance to all sites will be calculated.</span>
<span class="sd">            coords_are_cartesian (bool, optional): Whether the coordinates are</span>
<span class="sd">                in cartesian space. If using fractional coordinates set to</span>
<span class="sd">                False.</span>
<span class="sd">            round_dp (int, optional): The number of decimal places to round the</span>
<span class="sd">                miller index to.</span>
<span class="sd">            verbose (bool, optional): Whether to print warnings.</span>

<span class="sd">        Returns:</span>
<span class="sd">            (tuple): The Miller index.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="n">coords_are_cartesian</span><span class="p">:</span>
            <span class="n">coords</span> <span class="o">=</span> <span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">get_fractional_coords</span><span class="p">(</span><span class="n">c</span><span class="p">)</span> <span class="k">for</span> <span class="n">c</span> <span class="ow">in</span> <span class="n">coords</span><span class="p">]</span>

        <span class="n">coords</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">asarray</span><span class="p">(</span><span class="n">coords</span><span class="p">)</span>
        <span class="n">g</span> <span class="o">=</span> <span class="n">coords</span><span class="o">.</span><span class="n">sum</span><span class="p">(</span><span class="n">axis</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span> <span class="o">/</span> <span class="n">coords</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>

        <span class="c1"># run singular value decomposition</span>
        <span class="n">_</span><span class="p">,</span> <span class="n">_</span><span class="p">,</span> <span class="n">vh</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">linalg</span><span class="o">.</span><span class="n">svd</span><span class="p">(</span><span class="n">coords</span> <span class="o">-</span> <span class="n">g</span><span class="p">)</span>

        <span class="c1"># get unitary normal vector</span>
        <span class="n">u_norm</span> <span class="o">=</span> <span class="n">vh</span><span class="p">[</span><span class="mi">2</span><span class="p">,</span> <span class="p">:]</span>
        <span class="k">return</span> <span class="n">get_integer_index</span><span class="p">(</span><span class="n">u_norm</span><span class="p">,</span> <span class="n">round_dp</span><span class="o">=</span><span class="n">round_dp</span><span class="p">,</span> <span class="n">verbose</span><span class="o">=</span><span class="n">verbose</span><span class="p">)</span></div>

<div class="viewcode-block" id="Lattice.get_recp_symmetry_operation"><a class="viewcode-back" href="../../../pymatgen.core.lattice.html#pymatgen.core.lattice.Lattice.get_recp_symmetry_operation">[docs]</a>    <span class="k">def</span> <span class="nf">get_recp_symmetry_operation</span><span class="p">(</span>
            <span class="bp">self</span><span class="p">,</span> <span class="n">symprec</span><span class="p">:</span> <span class="nb">float</span> <span class="o">=</span> <span class="mf">0.01</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">List</span><span class="p">:</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Find the symmetric operations of the reciprocal lattice,</span>
<span class="sd">        to be used for hkl transformations</span>
<span class="sd">        Args:</span>
<span class="sd">            symprec: default is 0.001</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">recp_lattice</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">reciprocal_lattice_crystallographic</span>
        <span class="c1"># get symmetry operations from input conventional unit cell</span>
        <span class="c1"># Need to make sure recp lattice is big enough, otherwise symmetry</span>
        <span class="c1"># determination will fail. We set the overall volume to 1.</span>
        <span class="n">recp_lattice</span> <span class="o">=</span> <span class="n">recp_lattice</span><span class="o">.</span><span class="n">scale</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
        <span class="c1"># need a localized import of structure to build a</span>
        <span class="c1"># pseudo empty lattice for SpacegroupAnalyzer</span>
        <span class="kn">from</span> <span class="nn">pymatgen</span> <span class="kn">import</span> <span class="n">Structure</span>
        <span class="kn">from</span> <span class="nn">pymatgen.symmetry.analyzer</span> <span class="kn">import</span> <span class="n">SpacegroupAnalyzer</span>
        <span class="n">recp</span> <span class="o">=</span> <span class="n">Structure</span><span class="p">(</span><span class="n">recp_lattice</span><span class="p">,</span> <span class="p">[</span><span class="s2">&quot;H&quot;</span><span class="p">],</span> <span class="p">[[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">]])</span>
        <span class="c1"># Creates a function that uses the symmetry operations in the</span>
        <span class="c1"># structure to find Miller indices that might give repetitive slabs</span>
        <span class="n">analyzer</span> <span class="o">=</span> <span class="n">SpacegroupAnalyzer</span><span class="p">(</span><span class="n">recp</span><span class="p">,</span> <span class="n">symprec</span><span class="o">=</span><span class="n">symprec</span><span class="p">)</span>
        <span class="n">recp_symmops</span> <span class="o">=</span> <span class="n">analyzer</span><span class="o">.</span><span class="n">get_symmetry_operations</span><span class="p">()</span>

        <span class="k">return</span> <span class="n">recp_symmops</span></div></div>


<div class="viewcode-block" id="get_integer_index"><a class="viewcode-back" href="../../../pymatgen.core.lattice.html#pymatgen.core.lattice.get_integer_index">[docs]</a><span class="k">def</span> <span class="nf">get_integer_index</span><span class="p">(</span><span class="n">miller_index</span><span class="p">:</span> <span class="n">Sequence</span><span class="p">[</span><span class="nb">float</span><span class="p">],</span> <span class="n">round_dp</span><span class="p">:</span> <span class="nb">int</span> <span class="o">=</span> <span class="mi">4</span><span class="p">,</span> <span class="n">verbose</span><span class="p">:</span> <span class="nb">bool</span> <span class="o">=</span> <span class="kc">True</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">Tuple</span><span class="p">[</span><span class="nb">int</span><span class="p">,</span> <span class="nb">int</span><span class="p">,</span> <span class="nb">int</span><span class="p">]:</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Attempt to convert a vector of floats to whole numbers.</span>

<span class="sd">    Args:</span>
<span class="sd">        miller_index (list of float): A list miller indexes.</span>
<span class="sd">        round_dp (int, optional): The number of decimal places to round the</span>
<span class="sd">            miller index to.</span>
<span class="sd">        verbose (bool, optional): Whether to print warnings.</span>

<span class="sd">    Returns:</span>
<span class="sd">        (tuple): The Miller index.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">mi</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">asarray</span><span class="p">(</span><span class="n">miller_index</span><span class="p">)</span>
    <span class="c1"># deal with the case we have small irregular floats</span>
    <span class="c1"># that are all equal or factors of each other</span>
    <span class="n">mi</span> <span class="o">/=</span> <span class="nb">min</span><span class="p">([</span><span class="n">m</span> <span class="k">for</span> <span class="n">m</span> <span class="ow">in</span> <span class="n">mi</span> <span class="k">if</span> <span class="n">m</span> <span class="o">!=</span> <span class="mi">0</span><span class="p">])</span>
    <span class="n">mi</span> <span class="o">/=</span> <span class="n">np</span><span class="o">.</span><span class="n">max</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">abs</span><span class="p">(</span><span class="n">mi</span><span class="p">))</span>

    <span class="c1"># deal with the case we have nice fractions</span>
    <span class="n">md</span> <span class="o">=</span> <span class="p">[</span><span class="n">Fraction</span><span class="p">(</span><span class="n">n</span><span class="p">)</span><span class="o">.</span><span class="n">limit_denominator</span><span class="p">(</span><span class="mi">12</span><span class="p">)</span><span class="o">.</span><span class="n">denominator</span> <span class="k">for</span> <span class="n">n</span> <span class="ow">in</span> <span class="n">mi</span><span class="p">]</span>
    <span class="n">mi</span> <span class="o">*=</span> <span class="n">reduce</span><span class="p">(</span><span class="k">lambda</span> <span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">:</span> <span class="n">x</span> <span class="o">*</span> <span class="n">y</span><span class="p">,</span> <span class="n">md</span><span class="p">)</span>
    <span class="n">int_miller_index</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">int_</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">round</span><span class="p">(</span><span class="n">mi</span><span class="p">,</span> <span class="mi">1</span><span class="p">))</span>
    <span class="n">mi</span> <span class="o">/=</span> <span class="n">np</span><span class="o">.</span><span class="n">abs</span><span class="p">(</span><span class="n">reduce</span><span class="p">(</span><span class="n">math</span><span class="o">.</span><span class="n">gcd</span><span class="p">,</span> <span class="n">int_miller_index</span><span class="p">))</span>

    <span class="c1"># round to a reasonable precision</span>
    <span class="n">mi</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="nb">round</span><span class="p">(</span><span class="n">h</span><span class="p">,</span> <span class="n">round_dp</span><span class="p">)</span> <span class="k">for</span> <span class="n">h</span> <span class="ow">in</span> <span class="n">mi</span><span class="p">])</span>

    <span class="c1"># need to recalculate this after rounding as values may have changed</span>
    <span class="n">int_miller_index</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">int_</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">round</span><span class="p">(</span><span class="n">mi</span><span class="p">,</span> <span class="mi">1</span><span class="p">))</span>
    <span class="k">if</span> <span class="n">np</span><span class="o">.</span><span class="n">any</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">abs</span><span class="p">(</span><span class="n">mi</span> <span class="o">-</span> <span class="n">int_miller_index</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mf">1e-6</span><span class="p">)</span> <span class="ow">and</span> <span class="n">verbose</span><span class="p">:</span>
        <span class="n">warnings</span><span class="o">.</span><span class="n">warn</span><span class="p">(</span><span class="s2">&quot;Non-integer encountered in Miller index&quot;</span><span class="p">)</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="n">mi</span> <span class="o">=</span> <span class="n">int_miller_index</span>

    <span class="c1"># minimise the number of negative indexes</span>
    <span class="n">mi</span> <span class="o">+=</span> <span class="mi">0</span>  <span class="c1"># converts -0 to 0</span>

    <span class="k">def</span> <span class="nf">n_minus</span><span class="p">(</span><span class="n">index</span><span class="p">):</span>
        <span class="k">return</span> <span class="nb">len</span><span class="p">([</span><span class="n">h</span> <span class="k">for</span> <span class="n">h</span> <span class="ow">in</span> <span class="n">index</span> <span class="k">if</span> <span class="n">h</span> <span class="o">&lt;</span> <span class="mi">0</span><span class="p">])</span>

    <span class="k">if</span> <span class="n">n_minus</span><span class="p">(</span><span class="n">mi</span><span class="p">)</span> <span class="o">&gt;</span> <span class="n">n_minus</span><span class="p">(</span><span class="n">mi</span> <span class="o">*</span> <span class="o">-</span><span class="mi">1</span><span class="p">):</span>
        <span class="n">mi</span> <span class="o">*=</span> <span class="o">-</span><span class="mi">1</span>

    <span class="c1"># if only one index is negative, make sure it is the smallest</span>
    <span class="c1"># e.g. (-2 1 0) -&gt; (2 -1 0)</span>
    <span class="k">if</span> <span class="p">(</span>
            <span class="nb">sum</span><span class="p">(</span><span class="n">mi</span> <span class="o">!=</span> <span class="mi">0</span><span class="p">)</span> <span class="o">==</span> <span class="mi">2</span>
            <span class="ow">and</span> <span class="n">n_minus</span><span class="p">(</span><span class="n">mi</span><span class="p">)</span> <span class="o">==</span> <span class="mi">1</span>
            <span class="ow">and</span> <span class="nb">abs</span><span class="p">(</span><span class="nb">min</span><span class="p">(</span><span class="n">mi</span><span class="p">))</span> <span class="o">&gt;</span> <span class="nb">max</span><span class="p">(</span><span class="n">mi</span><span class="p">)</span>
    <span class="p">):</span>
        <span class="n">mi</span> <span class="o">*=</span> <span class="o">-</span><span class="mi">1</span>

    <span class="k">return</span> <span class="nb">tuple</span><span class="p">(</span><span class="n">mi</span><span class="p">)</span>  <span class="c1"># type: ignore</span></div>


<div class="viewcode-block" id="get_points_in_spheres"><a class="viewcode-back" href="../../../pymatgen.core.lattice.html#pymatgen.core.lattice.get_points_in_spheres">[docs]</a><span class="k">def</span> <span class="nf">get_points_in_spheres</span><span class="p">(</span><span class="n">all_coords</span><span class="p">:</span> <span class="n">np</span><span class="o">.</span><span class="n">ndarray</span><span class="p">,</span> <span class="n">center_coords</span><span class="p">:</span> <span class="n">np</span><span class="o">.</span><span class="n">ndarray</span><span class="p">,</span> <span class="n">r</span><span class="p">:</span> <span class="nb">float</span><span class="p">,</span>
                          <span class="n">pbc</span><span class="p">:</span> <span class="n">Union</span><span class="p">[</span><span class="nb">bool</span><span class="p">,</span> <span class="n">List</span><span class="p">[</span><span class="nb">bool</span><span class="p">]]</span> <span class="o">=</span> <span class="kc">True</span><span class="p">,</span> <span class="n">numerical_tol</span><span class="p">:</span> <span class="nb">float</span> <span class="o">=</span> <span class="mf">1e-8</span><span class="p">,</span>
                          <span class="n">lattice</span><span class="p">:</span> <span class="n">Lattice</span> <span class="o">=</span> <span class="kc">None</span><span class="p">,</span> <span class="n">return_fcoords</span><span class="p">:</span> <span class="nb">bool</span> <span class="o">=</span> <span class="kc">False</span><span class="p">,</span>
                          <span class="p">)</span> <span class="o">-&gt;</span> <span class="n">List</span><span class="p">[</span><span class="n">List</span><span class="p">[</span><span class="n">Tuple</span><span class="p">[</span><span class="n">np</span><span class="o">.</span><span class="n">ndarray</span><span class="p">,</span> <span class="nb">float</span><span class="p">,</span> <span class="nb">int</span><span class="p">,</span> <span class="n">np</span><span class="o">.</span><span class="n">ndarray</span><span class="p">]]]:</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    For each point in `center_coords`, get all the neighboring points in `all_coords` that are within the</span>
<span class="sd">    cutoff radius `r`.</span>

<span class="sd">    Args:</span>
<span class="sd">        all_coords: (list of cartesian coordinates) all available points</span>
<span class="sd">        center_coords: (list of cartesian coordinates) all centering points</span>
<span class="sd">        r: (float) cutoff radius</span>
<span class="sd">        pbc: (bool or a list of bool) whether to set periodic boundaries</span>
<span class="sd">        numerical_tol: (float) numerical tolerance</span>
<span class="sd">        lattice: (Lattice) lattice to consider when PBC is enabled</span>
<span class="sd">        return_fcoords: (bool) whether to return fractional coords when pbc is set.</span>
<span class="sd">    Returns:</span>
<span class="sd">        List[List[Tuple[coords, distance, index, image]]]</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">pbc</span><span class="p">,</span> <span class="nb">bool</span><span class="p">):</span>
        <span class="n">pbc</span> <span class="o">=</span> <span class="p">[</span><span class="n">pbc</span><span class="p">]</span> <span class="o">*</span> <span class="mi">3</span>
    <span class="n">pbc</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">pbc</span><span class="p">,</span> <span class="n">dtype</span><span class="o">=</span><span class="nb">bool</span><span class="p">)</span>
    <span class="k">if</span> <span class="n">return_fcoords</span> <span class="ow">and</span> <span class="n">lattice</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
        <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;Lattice needs to be supplied to compute fractional coordinates&quot;</span><span class="p">)</span>
    <span class="n">center_coords_min</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">min</span><span class="p">(</span><span class="n">center_coords</span><span class="p">,</span> <span class="n">axis</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span>
    <span class="n">center_coords_max</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">max</span><span class="p">(</span><span class="n">center_coords</span><span class="p">,</span> <span class="n">axis</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span>
    <span class="c1"># The lower bound of all considered atom coords</span>
    <span class="n">global_min</span> <span class="o">=</span> <span class="n">center_coords_min</span> <span class="o">-</span> <span class="n">r</span> <span class="o">-</span> <span class="n">numerical_tol</span>
    <span class="n">global_max</span> <span class="o">=</span> <span class="n">center_coords_max</span> <span class="o">+</span> <span class="n">r</span> <span class="o">+</span> <span class="n">numerical_tol</span>
    <span class="k">if</span> <span class="n">np</span><span class="o">.</span><span class="n">any</span><span class="p">(</span><span class="n">pbc</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">lattice</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;Lattice needs to be supplied when considering periodic boundary&quot;</span><span class="p">)</span>
        <span class="n">recp_len</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">lattice</span><span class="o">.</span><span class="n">reciprocal_lattice</span><span class="o">.</span><span class="n">abc</span><span class="p">)</span>
        <span class="n">maxr</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">ceil</span><span class="p">((</span><span class="n">r</span> <span class="o">+</span> <span class="mf">0.15</span><span class="p">)</span> <span class="o">*</span> <span class="n">recp_len</span> <span class="o">/</span> <span class="p">(</span><span class="mi">2</span> <span class="o">*</span> <span class="n">math</span><span class="o">.</span><span class="n">pi</span><span class="p">))</span>
        <span class="n">frac_coords</span> <span class="o">=</span> <span class="n">lattice</span><span class="o">.</span><span class="n">get_fractional_coords</span><span class="p">(</span><span class="n">center_coords</span><span class="p">)</span>
        <span class="n">nmin_temp</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">floor</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">min</span><span class="p">(</span><span class="n">frac_coords</span><span class="p">,</span> <span class="n">axis</span><span class="o">=</span><span class="mi">0</span><span class="p">))</span> <span class="o">-</span> <span class="n">maxr</span>
        <span class="n">nmax_temp</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">ceil</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">max</span><span class="p">(</span><span class="n">frac_coords</span><span class="p">,</span> <span class="n">axis</span><span class="o">=</span><span class="mi">0</span><span class="p">))</span> <span class="o">+</span> <span class="n">maxr</span>
        <span class="n">nmin</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros_like</span><span class="p">(</span><span class="n">nmin_temp</span><span class="p">)</span>
        <span class="n">nmin</span><span class="p">[</span><span class="n">pbc</span><span class="p">]</span> <span class="o">=</span> <span class="n">nmin_temp</span><span class="p">[</span><span class="n">pbc</span><span class="p">]</span>
        <span class="n">nmax</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">ones_like</span><span class="p">(</span><span class="n">nmax_temp</span><span class="p">)</span>
        <span class="n">nmax</span><span class="p">[</span><span class="n">pbc</span><span class="p">]</span> <span class="o">=</span> <span class="n">nmax_temp</span><span class="p">[</span><span class="n">pbc</span><span class="p">]</span>
        <span class="n">all_ranges</span> <span class="o">=</span> <span class="p">[</span><span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">dtype</span><span class="o">=</span><span class="s1">&#39;int64&#39;</span><span class="p">)</span> <span class="k">for</span> <span class="n">x</span><span class="p">,</span> <span class="n">y</span> <span class="ow">in</span> <span class="nb">zip</span><span class="p">(</span><span class="n">nmin</span><span class="p">,</span> <span class="n">nmax</span><span class="p">)]</span>
        <span class="n">matrix</span> <span class="o">=</span> <span class="n">lattice</span><span class="o">.</span><span class="n">matrix</span>
        <span class="c1"># temporarily hold the fractional coordinates</span>
        <span class="n">image_offsets</span> <span class="o">=</span> <span class="n">lattice</span><span class="o">.</span><span class="n">get_fractional_coords</span><span class="p">(</span><span class="n">all_coords</span><span class="p">)</span>
        <span class="n">all_fcoords</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="c1"># only wrap periodic boundary</span>
        <span class="k">for</span> <span class="n">k</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">3</span><span class="p">):</span>
            <span class="k">if</span> <span class="n">pbc</span><span class="p">[</span><span class="n">k</span><span class="p">]:</span>  <span class="c1"># type: ignore</span>
                <span class="n">all_fcoords</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">mod</span><span class="p">(</span><span class="n">image_offsets</span><span class="p">[:,</span> <span class="n">k</span><span class="p">:</span><span class="n">k</span><span class="o">+</span><span class="mi">1</span><span class="p">],</span> <span class="mi">1</span><span class="p">))</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">all_fcoords</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">image_offsets</span><span class="p">[:,</span> <span class="n">k</span><span class="p">:</span><span class="n">k</span><span class="o">+</span><span class="mi">1</span><span class="p">])</span>
        <span class="n">all_fcoords</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">concatenate</span><span class="p">(</span><span class="n">all_fcoords</span><span class="p">,</span> <span class="n">axis</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>
        <span class="n">image_offsets</span> <span class="o">=</span> <span class="n">image_offsets</span> <span class="o">-</span> <span class="n">all_fcoords</span>
        <span class="n">coords_in_cell</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">dot</span><span class="p">(</span><span class="n">all_fcoords</span><span class="p">,</span> <span class="n">matrix</span><span class="p">)</span>
        <span class="c1"># Filter out those beyond max range</span>
        <span class="n">valid_coords</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="n">valid_images</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="n">valid_indices</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">for</span> <span class="n">image</span> <span class="ow">in</span> <span class="n">itertools</span><span class="o">.</span><span class="n">product</span><span class="p">(</span><span class="o">*</span><span class="n">all_ranges</span><span class="p">):</span>
            <span class="n">coords</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">dot</span><span class="p">(</span><span class="n">image</span><span class="p">,</span> <span class="n">matrix</span><span class="p">)</span> <span class="o">+</span> <span class="n">coords_in_cell</span>
            <span class="n">valid_index_bool</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">all</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">bitwise_and</span><span class="p">(</span><span class="n">coords</span> <span class="o">&gt;</span> <span class="n">global_min</span><span class="p">[</span><span class="kc">None</span><span class="p">,</span> <span class="p">:],</span> <span class="n">coords</span> <span class="o">&lt;</span> <span class="n">global_max</span><span class="p">[</span><span class="kc">None</span><span class="p">,</span> <span class="p">:]),</span>
                                      <span class="n">axis</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>
            <span class="n">ind</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">all_coords</span><span class="p">))</span>
            <span class="k">if</span> <span class="n">np</span><span class="o">.</span><span class="n">any</span><span class="p">(</span><span class="n">valid_index_bool</span><span class="p">):</span>
                <span class="n">valid_coords</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">coords</span><span class="p">[</span><span class="n">valid_index_bool</span><span class="p">])</span>
                <span class="n">valid_images</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">tile</span><span class="p">(</span><span class="n">image</span><span class="p">,</span> <span class="p">[</span><span class="n">np</span><span class="o">.</span><span class="n">sum</span><span class="p">(</span><span class="n">valid_index_bool</span><span class="p">),</span> <span class="mi">1</span><span class="p">])</span> <span class="o">-</span> <span class="n">image_offsets</span><span class="p">[</span><span class="n">valid_index_bool</span><span class="p">])</span>
                <span class="n">valid_indices</span><span class="o">.</span><span class="n">extend</span><span class="p">([</span><span class="n">k</span> <span class="k">for</span> <span class="n">k</span> <span class="ow">in</span> <span class="n">ind</span> <span class="k">if</span> <span class="n">valid_index_bool</span><span class="p">[</span><span class="n">k</span><span class="p">]])</span>
        <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">valid_coords</span><span class="p">)</span> <span class="o">&lt;</span> <span class="mi">1</span><span class="p">:</span>
            <span class="k">return</span> <span class="p">[[]]</span> <span class="o">*</span> <span class="nb">len</span><span class="p">(</span><span class="n">center_coords</span><span class="p">)</span>
        <span class="n">valid_coords</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">concatenate</span><span class="p">(</span><span class="n">valid_coords</span><span class="p">,</span> <span class="n">axis</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span>
        <span class="n">valid_images</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">concatenate</span><span class="p">(</span><span class="n">valid_images</span><span class="p">,</span> <span class="n">axis</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span>

    <span class="k">else</span><span class="p">:</span>
        <span class="n">valid_coords</span> <span class="o">=</span> <span class="n">all_coords</span>
        <span class="n">valid_images</span> <span class="o">=</span> <span class="p">[[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">]]</span> <span class="o">*</span> <span class="nb">len</span><span class="p">(</span><span class="n">valid_coords</span><span class="p">)</span>
        <span class="n">valid_indices</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">valid_coords</span><span class="p">))</span>

    <span class="c1"># Divide the valid 3D space into cubes and compute the cube ids</span>
    <span class="n">all_cube_index</span> <span class="o">=</span> <span class="n">_compute_cube_index</span><span class="p">(</span><span class="n">valid_coords</span><span class="p">,</span> <span class="n">global_min</span><span class="p">,</span> <span class="n">r</span><span class="p">)</span>
    <span class="n">nx</span><span class="p">,</span> <span class="n">ny</span><span class="p">,</span> <span class="n">nz</span> <span class="o">=</span> <span class="n">_compute_cube_index</span><span class="p">(</span><span class="n">global_max</span><span class="p">,</span> <span class="n">global_min</span><span class="p">,</span> <span class="n">r</span><span class="p">)</span> <span class="o">+</span> <span class="mi">1</span>
    <span class="n">all_cube_index</span> <span class="o">=</span> <span class="n">_three_to_one</span><span class="p">(</span><span class="n">all_cube_index</span><span class="p">,</span> <span class="n">ny</span><span class="p">,</span> <span class="n">nz</span><span class="p">)</span>
    <span class="n">site_cube_index</span> <span class="o">=</span> <span class="n">_three_to_one</span><span class="p">(</span><span class="n">_compute_cube_index</span><span class="p">(</span><span class="n">center_coords</span><span class="p">,</span> <span class="n">global_min</span><span class="p">,</span> <span class="n">r</span><span class="p">),</span> <span class="n">ny</span><span class="p">,</span> <span class="n">nz</span><span class="p">)</span>
    <span class="c1"># create cube index to coordinates, images, and indices map</span>
    <span class="n">cube_to_coords</span> <span class="o">=</span> <span class="n">collections</span><span class="o">.</span><span class="n">defaultdict</span><span class="p">(</span><span class="nb">list</span><span class="p">)</span>  <span class="c1"># type: Dict[int, List]</span>
    <span class="n">cube_to_images</span> <span class="o">=</span> <span class="n">collections</span><span class="o">.</span><span class="n">defaultdict</span><span class="p">(</span><span class="nb">list</span><span class="p">)</span>  <span class="c1"># type: Dict[int, List]</span>
    <span class="n">cube_to_indices</span> <span class="o">=</span> <span class="n">collections</span><span class="o">.</span><span class="n">defaultdict</span><span class="p">(</span><span class="nb">list</span><span class="p">)</span>  <span class="c1"># type: Dict[int, List]</span>
    <span class="k">for</span> <span class="n">i</span><span class="p">,</span> <span class="n">j</span><span class="p">,</span> <span class="n">k</span><span class="p">,</span> <span class="n">l</span> <span class="ow">in</span> <span class="nb">zip</span><span class="p">(</span><span class="n">all_cube_index</span><span class="o">.</span><span class="n">ravel</span><span class="p">(),</span> <span class="n">valid_coords</span><span class="p">,</span>
                          <span class="n">valid_images</span><span class="p">,</span> <span class="n">valid_indices</span><span class="p">):</span>
        <span class="n">cube_to_coords</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">j</span><span class="p">)</span>
        <span class="n">cube_to_images</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">k</span><span class="p">)</span>
        <span class="n">cube_to_indices</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">l</span><span class="p">)</span>

    <span class="c1"># find all neighboring cubes for each atom in the lattice cell</span>
    <span class="n">site_neighbors</span> <span class="o">=</span> <span class="n">find_neighbors</span><span class="p">(</span><span class="n">site_cube_index</span><span class="p">,</span> <span class="n">nx</span><span class="p">,</span> <span class="n">ny</span><span class="p">,</span> <span class="n">nz</span><span class="p">)</span>
    <span class="n">neighbors</span> <span class="o">=</span> <span class="p">[]</span>  <span class="c1"># type: List[List[Tuple[np.ndarray, float, int, np.ndarray]]]</span>

    <span class="k">for</span> <span class="n">i</span><span class="p">,</span> <span class="n">j</span> <span class="ow">in</span> <span class="nb">zip</span><span class="p">(</span><span class="n">center_coords</span><span class="p">,</span> <span class="n">site_neighbors</span><span class="p">):</span>
        <span class="n">l1</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">_three_to_one</span><span class="p">(</span><span class="n">j</span><span class="p">,</span> <span class="n">ny</span><span class="p">,</span> <span class="n">nz</span><span class="p">),</span> <span class="n">dtype</span><span class="o">=</span><span class="nb">int</span><span class="p">)</span><span class="o">.</span><span class="n">ravel</span><span class="p">()</span>
        <span class="c1"># use the cube index map to find the all the neighboring</span>
        <span class="c1"># coords, images, and indices</span>
        <span class="n">ks</span> <span class="o">=</span> <span class="p">[</span><span class="n">k</span> <span class="k">for</span> <span class="n">k</span> <span class="ow">in</span> <span class="n">l1</span> <span class="k">if</span> <span class="n">k</span> <span class="ow">in</span> <span class="n">cube_to_coords</span><span class="p">]</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">ks</span><span class="p">:</span>
            <span class="n">neighbors</span><span class="o">.</span><span class="n">append</span><span class="p">([])</span>
            <span class="k">continue</span>
        <span class="n">nn_coords</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">concatenate</span><span class="p">([</span><span class="n">cube_to_coords</span><span class="p">[</span><span class="n">k</span><span class="p">]</span> <span class="k">for</span> <span class="n">k</span> <span class="ow">in</span> <span class="n">ks</span><span class="p">],</span> <span class="n">axis</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span>
        <span class="n">nn_images</span> <span class="o">=</span> <span class="n">itertools</span><span class="o">.</span><span class="n">chain</span><span class="p">(</span><span class="o">*</span><span class="p">[</span><span class="n">cube_to_images</span><span class="p">[</span><span class="n">k</span><span class="p">]</span> <span class="k">for</span> <span class="n">k</span> <span class="ow">in</span> <span class="n">ks</span><span class="p">])</span>
        <span class="n">nn_indices</span> <span class="o">=</span> <span class="n">itertools</span><span class="o">.</span><span class="n">chain</span><span class="p">(</span><span class="o">*</span><span class="p">[</span><span class="n">cube_to_indices</span><span class="p">[</span><span class="n">k</span><span class="p">]</span> <span class="k">for</span> <span class="n">k</span> <span class="ow">in</span> <span class="n">ks</span><span class="p">])</span>
        <span class="n">dist</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">linalg</span><span class="o">.</span><span class="n">norm</span><span class="p">(</span><span class="n">nn_coords</span> <span class="o">-</span> <span class="n">i</span><span class="p">[</span><span class="kc">None</span><span class="p">,</span> <span class="p">:],</span> <span class="n">axis</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>
        <span class="n">nns</span><span class="p">:</span> <span class="n">List</span><span class="p">[</span><span class="n">Tuple</span><span class="p">[</span><span class="n">np</span><span class="o">.</span><span class="n">ndarray</span><span class="p">,</span> <span class="nb">float</span><span class="p">,</span> <span class="nb">int</span><span class="p">,</span> <span class="n">np</span><span class="o">.</span><span class="n">ndarray</span><span class="p">]]</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">for</span> <span class="n">coord</span><span class="p">,</span> <span class="n">index</span><span class="p">,</span> <span class="n">image</span><span class="p">,</span> <span class="n">d</span> <span class="ow">in</span> <span class="nb">zip</span><span class="p">(</span><span class="n">nn_coords</span><span class="p">,</span> <span class="n">nn_indices</span><span class="p">,</span> <span class="n">nn_images</span><span class="p">,</span> <span class="n">dist</span><span class="p">):</span>
            <span class="c1"># filtering out all sites that are beyond the cutoff</span>
            <span class="c1"># Here there is no filtering of overlapping sites</span>
            <span class="k">if</span> <span class="n">d</span> <span class="o">&lt;</span> <span class="n">r</span> <span class="o">+</span> <span class="n">numerical_tol</span><span class="p">:</span>
                <span class="k">if</span> <span class="n">return_fcoords</span> <span class="ow">and</span> <span class="p">(</span><span class="n">lattice</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">):</span>
                    <span class="n">coord</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">round</span><span class="p">(</span><span class="n">lattice</span><span class="o">.</span><span class="n">get_fractional_coords</span><span class="p">(</span><span class="n">coord</span><span class="p">),</span> <span class="mi">10</span><span class="p">)</span>
                <span class="n">nn</span> <span class="o">=</span> <span class="p">(</span><span class="n">coord</span><span class="p">,</span> <span class="nb">float</span><span class="p">(</span><span class="n">d</span><span class="p">),</span> <span class="nb">int</span><span class="p">(</span><span class="n">index</span><span class="p">),</span> <span class="n">image</span><span class="p">)</span>
                <span class="n">nns</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">nn</span><span class="p">)</span>
        <span class="n">neighbors</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">nns</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">neighbors</span></div>


<span class="c1"># The following internal methods are used in the get_points_in_sphere method.</span>
<span class="k">def</span> <span class="nf">_compute_cube_index</span><span class="p">(</span><span class="n">coords</span><span class="p">:</span> <span class="n">np</span><span class="o">.</span><span class="n">ndarray</span><span class="p">,</span> <span class="n">global_min</span><span class="p">:</span> <span class="nb">float</span><span class="p">,</span> <span class="n">radius</span><span class="p">:</span> <span class="nb">float</span>
                        <span class="p">)</span> <span class="o">-&gt;</span> <span class="n">np</span><span class="o">.</span><span class="n">ndarray</span><span class="p">:</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Compute the cube index from coordinates</span>
<span class="sd">    Args:</span>
<span class="sd">        coords: (nx3 array) atom coordinates</span>
<span class="sd">        global_min: (float) lower boundary of coordinates</span>
<span class="sd">        radius: (float) cutoff radius</span>

<span class="sd">    Returns: (nx3 array) int indices</span>

<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">return</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">floor</span><span class="p">((</span><span class="n">coords</span> <span class="o">-</span> <span class="n">global_min</span><span class="p">)</span> <span class="o">/</span> <span class="n">radius</span><span class="p">),</span> <span class="n">dtype</span><span class="o">=</span><span class="nb">int</span><span class="p">)</span>


<span class="k">def</span> <span class="nf">_one_to_three</span><span class="p">(</span><span class="n">label1d</span><span class="p">:</span> <span class="n">np</span><span class="o">.</span><span class="n">ndarray</span><span class="p">,</span> <span class="n">ny</span><span class="p">:</span> <span class="nb">int</span><span class="p">,</span> <span class="n">nz</span><span class="p">:</span> <span class="nb">int</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">np</span><span class="o">.</span><span class="n">ndarray</span><span class="p">:</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Convert a 1D index array to 3D index array</span>

<span class="sd">    Args:</span>
<span class="sd">        label1d: (array) 1D index array</span>
<span class="sd">        ny: (int) number of cells in y direction</span>
<span class="sd">        nz: (int) number of cells in z direction</span>

<span class="sd">    Returns: (nx3) int array of index</span>

<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">last</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">mod</span><span class="p">(</span><span class="n">label1d</span><span class="p">,</span> <span class="n">nz</span><span class="p">)</span>
    <span class="n">second</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">mod</span><span class="p">((</span><span class="n">label1d</span> <span class="o">-</span> <span class="n">last</span><span class="p">)</span> <span class="o">/</span> <span class="n">nz</span><span class="p">,</span> <span class="n">ny</span><span class="p">)</span>
    <span class="n">first</span> <span class="o">=</span> <span class="p">(</span><span class="n">label1d</span> <span class="o">-</span> <span class="n">last</span> <span class="o">-</span> <span class="n">second</span> <span class="o">*</span> <span class="n">nz</span><span class="p">)</span> <span class="o">/</span> <span class="p">(</span><span class="n">ny</span> <span class="o">*</span> <span class="n">nz</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">np</span><span class="o">.</span><span class="n">concatenate</span><span class="p">([</span><span class="n">first</span><span class="p">,</span> <span class="n">second</span><span class="p">,</span> <span class="n">last</span><span class="p">],</span> <span class="n">axis</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>


<span class="k">def</span> <span class="nf">_three_to_one</span><span class="p">(</span><span class="n">label3d</span><span class="p">:</span> <span class="n">np</span><span class="o">.</span><span class="n">ndarray</span><span class="p">,</span> <span class="n">ny</span><span class="p">:</span> <span class="nb">int</span><span class="p">,</span> <span class="n">nz</span><span class="p">:</span> <span class="nb">int</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">np</span><span class="o">.</span><span class="n">ndarray</span><span class="p">:</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    The reverse of _one_to_three</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">return</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">label3d</span><span class="p">[:,</span> <span class="mi">0</span><span class="p">]</span> <span class="o">*</span> <span class="n">ny</span> <span class="o">*</span> <span class="n">nz</span> <span class="o">+</span>
                    <span class="n">label3d</span><span class="p">[:,</span> <span class="mi">1</span><span class="p">]</span> <span class="o">*</span> <span class="n">nz</span> <span class="o">+</span> <span class="n">label3d</span><span class="p">[:,</span> <span class="mi">2</span><span class="p">])</span><span class="o">.</span><span class="n">reshape</span><span class="p">((</span><span class="o">-</span><span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">))</span>


<div class="viewcode-block" id="find_neighbors"><a class="viewcode-back" href="../../../pymatgen.core.lattice.html#pymatgen.core.lattice.find_neighbors">[docs]</a><span class="k">def</span> <span class="nf">find_neighbors</span><span class="p">(</span><span class="n">label</span><span class="p">:</span> <span class="n">np</span><span class="o">.</span><span class="n">ndarray</span><span class="p">,</span> <span class="n">nx</span><span class="p">:</span> <span class="nb">int</span><span class="p">,</span> <span class="n">ny</span><span class="p">:</span> <span class="nb">int</span><span class="p">,</span> <span class="n">nz</span><span class="p">:</span> <span class="nb">int</span>
                   <span class="p">)</span> <span class="o">-&gt;</span> <span class="n">List</span><span class="p">[</span><span class="n">np</span><span class="o">.</span><span class="n">ndarray</span><span class="p">]:</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Given a cube index, find the neighbor cube indices</span>

<span class="sd">    Args:</span>
<span class="sd">        label: (array) (n,) or (n x 3) indice array</span>
<span class="sd">        nx: (int) number of cells in y direction</span>
<span class="sd">        ny: (int) number of cells in y direction</span>
<span class="sd">        nz: (int) number of cells in z direction</span>

<span class="sd">    Returns: neighbor cell indices</span>

<span class="sd">    &quot;&quot;&quot;</span>

    <span class="n">array</span> <span class="o">=</span> <span class="p">[[</span><span class="o">-</span><span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">]]</span> <span class="o">*</span> <span class="mi">3</span>
    <span class="n">neighbor_vectors</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="nb">list</span><span class="p">(</span><span class="n">itertools</span><span class="o">.</span><span class="n">product</span><span class="p">(</span><span class="o">*</span><span class="n">array</span><span class="p">)),</span>
                                <span class="n">dtype</span><span class="o">=</span><span class="nb">int</span><span class="p">)</span>
    <span class="k">if</span> <span class="n">np</span><span class="o">.</span><span class="n">shape</span><span class="p">(</span><span class="n">label</span><span class="p">)[</span><span class="mi">1</span><span class="p">]</span> <span class="o">==</span> <span class="mi">1</span><span class="p">:</span>
        <span class="n">label3d</span> <span class="o">=</span> <span class="n">_one_to_three</span><span class="p">(</span><span class="n">label</span><span class="p">,</span> <span class="n">ny</span><span class="p">,</span> <span class="n">nz</span><span class="p">)</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="n">label3d</span> <span class="o">=</span> <span class="n">label</span>
    <span class="n">all_labels</span> <span class="o">=</span> <span class="n">label3d</span><span class="p">[:,</span> <span class="kc">None</span><span class="p">,</span> <span class="p">:]</span> <span class="o">-</span> <span class="n">neighbor_vectors</span><span class="p">[</span><span class="kc">None</span><span class="p">,</span> <span class="p">:,</span> <span class="p">:]</span>
    <span class="n">filtered_labels</span> <span class="o">=</span> <span class="p">[]</span>
    <span class="c1"># filter out out-of-bound labels i.e., label &lt; 0</span>
    <span class="k">for</span> <span class="n">labels</span> <span class="ow">in</span> <span class="n">all_labels</span><span class="p">:</span>
        <span class="n">ind</span> <span class="o">=</span> <span class="p">(</span><span class="n">labels</span><span class="p">[:,</span> <span class="mi">0</span><span class="p">]</span> <span class="o">&lt;</span> <span class="n">nx</span><span class="p">)</span> <span class="o">*</span> <span class="p">(</span><span class="n">labels</span><span class="p">[:,</span> <span class="mi">1</span><span class="p">]</span> <span class="o">&lt;</span> <span class="n">ny</span><span class="p">)</span> <span class="o">*</span> <span class="p">(</span><span class="n">labels</span><span class="p">[:,</span> <span class="mi">2</span><span class="p">]</span> <span class="o">&lt;</span> <span class="n">nz</span><span class="p">)</span> <span class="o">*</span> <span class="n">np</span><span class="o">.</span><span class="n">all</span><span class="p">(</span><span class="n">labels</span> <span class="o">&gt;</span> <span class="o">-</span><span class="mf">1e-5</span><span class="p">,</span> <span class="n">axis</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>
        <span class="n">filtered_labels</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">labels</span><span class="p">[</span><span class="n">ind</span><span class="p">])</span>
    <span class="k">return</span> <span class="n">filtered_labels</span></div>
</pre></div>

            <div class="clearer"></div>
          </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="nav-item nav-item-0"><a href="../../../index.html">pymatgen 2020.7.3 documentation</a> &#187;</li>
          <li class="nav-item nav-item-1"><a href="../../index.html" >Module code</a> &#187;</li>
          <li class="nav-item nav-item-2"><a href="../../pymatgen.html" >pymatgen</a> &#187;</li>
        <li class="nav-item nav-item-this"><a href="">pymatgen.core.lattice</a></li> 
      </ul>
    </div>

    <div class="footer" role="contentinfo">
        &#169; Copyright 2011, Pymatgen Development Team.
      Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 3.1.2.
    </div>
<div class="footer">This page uses <a href="http://analytics.google.com/">
Google Analytics</a> to collect statistics. You can disable it by blocking
the JavaScript coming from www.google-analytics.com.
<script type="text/javascript">
  (function() {
    var ga = document.createElement('script');
    ga.src = ('https:' == document.location.protocol ?
              'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    ga.setAttribute('async', 'true');
    document.documentElement.firstChild.appendChild(ga);
  })();
</script>
</div>

  </body>
</html>